Need help in character pointer

Hi,

I am trying to divide my input to different type of out puts for some other use.

ex: logical_name : jkl00001

expected out put : model=jkl and num=00001

here is the code i actually written

//
void update_new_logical_name(char logical_name)
/
**/
{
int next=0,ret=0,n;
char model[4];
int num[6];
char *name;

name=logical_name;


            for\(n=0;n<3;n\+\+\)
            \{
            model[n]=name[n];
   
            \}

            model[4]='\\0';
             for\(n=3;n<8;n\+\+\)
             \{
               num[n-3]=name[n];
              \}
               num[6]='\\0';

#ifdef DEBUG

printf("\n\nmodel== '%s',number=='%d'\n",model,num);

#endif

}

by using the above code i am not able to print anything .num displays some address and model displays nothing.

Some body please correct me

I need it urgently.
Thx,
jagan

#include <string.h>
char *update_new_logical_name(char *src)
{
      char part_one[6]={0x0};
      char part_two[12]={0x0};
      if(strlen(src)>=3)
          memcpy(part_one, src, 3);
      if(strlen(src)>=8)
          memcpy(part_two, &src[3], 5);
      printf ("model = %s  num = %s\n, part_one, part_two);
      return part_two;
     }

Can you list what rules needs to be followed for parsing the logical name. Something like...

model part is always alphabetic and 3 chars long.
num part is always numeric and 5 digits long.

First mistake

int num[6];

should be

char num[6];

if you want to copy a string into it.
The lines

model[4]='\0';
...
num[6]='\0';

write past the end of your arrays.
You also need to change the formatting in the printf; it should be

printf("\n\nmodel== '%s',number=='%s'\n",model,num);

Jim's solution covers all of this but does not ensure that the strings in part_one and part_two are null-terminated:

char part_one[6]={0x0};

sets only the first element to 0.
I would use strncpy(3) instead which pads out any remaining space in the destination array with nulls.

Hi,

Thanks for the reply..
yes the model part should be 3 alphabetic chars and number should be 5 numerical characters.

I need do pass these variables to some other fn like this

db_insert_last_log_name(char *model,int number)

Thx,
jagan

spirtle -

char x[10]={0x0};

According to C99 standard all of the 10 elements of x are set to zero. Unless you are using ancient Solaris compilers or something.... try it with this junk code in debug mode:
edit: see para 21 in section 6.7.8 of C99 standards.

void foo(void)
{
    int b[3]={0};
    char c[4]={0x0};
	/* this point */
}

int main()
{ 
    foo(); 
    return 0;
}

break on the line # for /* this point */

If you are going to pass numerics as an int then in the example the number part of the logical name will go from 00001 to 1 as leading zeros will be removed from an int. Is that what you want or do you want to pass a numerical string --> 00001 ??

Hi,

its ok to pass without leading zeroes. I just need that number

thanks,
jagan

I have very lightly changed jim mcnamara's code...hope he is :cool: with that.
As the model and num variables are going to be used in other functions make them global and the update_new_logical_name() function need not return anything.

#include <string.h>
char part_one[3];
int num;
void update_new_logical_name(char *src)
{
      if(strlen(src)>=3)
          memcpy(part_one, src, 3);
      if(strlen(src)>=8)
          num = atoi(src+3);      
      printf ("model = %s  num = %s\n, part_one, num);
}

Fair enough! Unfortunately, I have to work with stone-age C, so I tend to overlook these fancy modern features.

Hi,

Thanks for the reply...........

I tried with the below given code and could not see any integer number stored at num. seems some more changes needed...

Thx,
jagan

Post the code that you are using...since the one that you are using isn't giving the desired output. Given the string jkl00001 the code in my last post works correctly. It will be incorrect only if the input string changed. Is that the case?