i m confused about sprint syntax- i need to create dynamic mysql query so i have to use it, when i take char * it shows a error while when i take char arr[100] (like this )it work fine..
i read syntax from sprint from multiple sites, and it make myself confuse, i think char pointer is best option because here we odnt need to give the size, but it does not work in my case, can we not use char pointer as first argument of sprint function......
my main problem is , i made a function-

char * d_generate_usr_key_generate_usrkey();
and it's defination like this:-
char * d_generate_usr_key_generate_usrkey()
{
 char new_key[8];
sprintf(new_key,"%s0001",fixed_char);// char * fixed_char="amit"
1-printf("%s",new_key);
return new_key;
}

and i m calling it as

char *new_predefined_usr_key;
	new_predefined_usr_key=table_object->d_generate_usr_key_generate_usrkey();
	2-printf("\n%s",new_predefined_usr_key);

here in this condition it is printing amit0001 two times, no problem with this, but when i m removing 1 printf from inside the function this 2 printf give garbage value, How this is happening.. Pls let me know where i m wrong!!!!

Edited 5 Years Ago by WaltP: Added CODE Tgs -- plese use them.

i m confused about sprint syntax- i need to create dynamic mysql query so i have to use it, when i take char * it shows a error while when i take char arr[100] (like this )it work fine..
i read syntax from sprint from multiple sites, and it make myself confuse, i think char pointer is best option because here we odnt need to give the size, but it does not work in my case, can we not use char pointer as first argument of sprint function......
my main problem is , i made a function-

char * d_generate_usr_key_generate_usrkey();
and it's defination like this:-
char * d_generate_usr_key_generate_usrkey()
{
 char new_key[8];
sprintf(new_key,"%s0001",fixed_char);// char * fixed_char="amit"
1-printf("%s",new_key);
return new_key;
}

and i m calling it as

char *new_predefined_usr_key;
	new_predefined_usr_key=table_object->d_generate_usr_key_generate_usrkey();
	2-printf("\n%s",new_predefined_usr_key);

here in this condition it is printing amit0001 two times, no problem with this, but when i m removing 1 printf from inside the function this 2 printf give garbage value, How this is happening.. Pls let me know where i m wrong!!!!

Finally i got the solution, my first problem was, what to use for sprintf, so u can use both
1- when u use char arr[100] ; it means u create a buffer of 100 char and pass it to sprintf,
2 when u will use char *arr;[ it does not mean memory buffer, it is just a pointer ], In this case u need to define this by
--

arr= (char *)calloc( 100, sizeof(char));

--
now u can use this,
My second problem related to print statement, it is happening because there i was using the concept of char array and when i returning this, it laps because i create it inside the method and it scope is only inside the method ,so if i use char pointer here then we can pass it easily... really i got somethng today.. thank you all

This question has already been answered. Start a new discussion instead.