Need help in character pointer


 
Thread Tools Search this Thread
Top Forums Programming Need help in character pointer
# 1  
Old 09-12-2008
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
# 2  
Old 09-12-2008
Code:
#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;
     }


Last edited by jim mcnamara; 09-12-2008 at 01:00 PM..
# 3  
Old 09-12-2008
Parse rules

Can you list what rules needs to be followed for parsing the logical name. Something like...
Code:
model part is always alphabetic and 3 chars long.
num part is always numeric and 5 digits long.

# 4  
Old 09-12-2008
First mistake
Code:
int num[6];

should be
Code:
char num[6];

if you want to copy a string into it.
The lines
Code:
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
Code:
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:
Code:
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.
# 5  
Old 09-12-2008
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
# 6  
Old 09-12-2008
spirtle -
Code:
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.
Code:
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 */

Last edited by jim mcnamara; 09-12-2008 at 06:31 PM.. Reason: couldn't find reference got it now
# 7  
Old 09-12-2008
Question Clarify

Quote:
Originally Posted by jagan_kalluri
I need do pass these variables to some other fn like this

db_insert_last_log_name(char *model,int number)
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 ??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Character pointer to Character array

how to copy content of character pointer to character array in c programming.. char *num; char name=num; (1 Reply)
Discussion started by: zinat
1 Replies

2. Programming

Pointer confusion

Here are two programs that pass a pointer to a variable but behave differently. Shouldnt the i in second program be 0 after the function call? #include<stdio.h> void changeI(int *i) { *i = 10; } int main(void) { int i=5; printf("%d before\n", i); changeI(&i); printf("%d... (1 Reply)
Discussion started by: dragonpoint
1 Replies

3. Programming

pointer problem

Does anyone know? int x = 1; int *p = &++x; //ok ! int *q = &x++; //gives an error :O why the first pointer is ok but the second is an error? (13 Replies)
Discussion started by: nishrestha
13 Replies

4. Programming

Pointer to pointers

Hi guys, I'm trying to understand pointers in C and made a simple example and I've problems with It. Can someone help? #include <stdio.h> #include <stdlib.h> #include <assert.h> int f1(char **str_); int main(int argc, char **argv) { char *str = NULL; f1(&str); ... (3 Replies)
Discussion started by: pharaoh
3 Replies

5. Programming

C dynamic pointer

Hi, Can anyone tell me how i can declare and allocate dynamically an array of pointers to structured type?? Is declaration something like this:? struct_name ** array; (1 Reply)
Discussion started by: littleboyblu
1 Replies

6. Programming

matrix pointer

Can anyone tell me what the following statements do? float (*tab); tab=(float (*)) calloc(MAXCLASS, (MAXCLASS+1)*sizeof(float)); (3 Replies)
Discussion started by: littleboyblu
3 Replies

7. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

8. Programming

far pointer

what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies

9. Programming

why we never delete a pointer twice

can u tell me the reson that why we should not delete a pointer twice.? if we delete ponter twice then what happen and why this happen Regards, Amit (2 Replies)
Discussion started by: amitpansuria
2 Replies

10. Programming

pointer

void main() { int a={1,2,3,4,5,6,7,8,9,10}; int *p=a; int *q=&a; cout<<q-p+1<<endl; } The output is 10, how? if we give cout<<q it will print the address, value won't print.... if we give cout<<p it will print the address, value won't print.... p has the base addr; q... (1 Reply)
Discussion started by: sarwan
1 Replies
Login or Register to Ask a Question