Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string copy function 4

Status
Not open for further replies.

manichandra

Technical User
Feb 6, 2012
19
US
what is wrong with this code? its not working . can anybody explain in detail please ???
____________________________

#include <stdio.h>
#include<stdlib.h>
#include<string.h>

void newStrCpy(char* q, char* p);

int main (void)
{

char *q = "MANICHANDRA";

char *p ;

newStrCpy(q,p);

//printf("%d",(int)p);

printf("%s\n",p);

return 0;
}

void newStrCpy(char* q, char* p)
{

//printf("%d",(int)p);

char *temp =(char*) malloc(sizeof(*q));

int i=0;

do{

*(temp+i) = *(q+i);

i++;

}

while(*(q+i)!='\0');

//printf("%s",temp);

p = temp;

}
 
Code:
 char *temp =(char*) malloc(sizeof(*q));

This allocates 1 byte. You need
Code:
char *temp = (char*) malloc (strlen(q) + 1);
 
Thanks xwb , i tried by
char *temp = (char*) malloc (strlen(q) + 1);

but still the output is "null"..

in my code i checked the value of "p" before going out of the function by debugging.
at this point "p" is pointing to "temp".

But once the compiler goes out of the function p is pointing to the initial value.
 
If you wish to change the value of p, then you need to pass it as a pointer to the char*
Code:
void newStrCpy(char* q, char** p)
{
...

   *p = temp;
}



 
Some additions:

1. There is a wonderful operator in C. It's defined as
Code:
a[expr] <=> *(a+(expr))
Now you may write much more clear codes with
Code:
p[i] /*instead of correct but cumbersome *(p+i) */

2. String literal "..." is a special kind of array of chars in C. The content of "abc" is exactly the same as
Code:
{ 'a', 'b', 'c', '\0' } /* constant array of four chars */
By historical reasons it's possible to get non-constant pointer to this array in C but any correct program can't assign values via such a pointer.
Now let's remember that an array is implicitly converted to the pointer to the 1st element of this array (everywhere except sizeof and address of (unary &) operators argument). That's why you can write this declaration:
Code:
char* q = "MANICHANDRA";
/* Better write more correctly as below: */
const char* q = "MANICHANDRA";
Now q is a pointer to the 1st character of (constant!) array of chars (to 'M' letter in this case).
Compare with the different construct:
Code:
char s[] = "MAN";
This is a special kind of an array declaration with initialization. After that s is a name of array of 4 characters. It's the same as
Code:
char s[4] = { 'M', 'A', 'N', '\0' }; /* see zero byte terminator */
Feel free to change all four elements of s:
Code:
s[0], s[1], s[2], s[3]

3. Function arguments are passed "by value" in C. In other words, a function receives copies of argument values. Let's consider
Code:
void f(char* p)
{
   char s[] = "MAN";
   p = s;
}
...
char ss[] = "WOMAN";
f(ss);
printf("%s\n",ss); /* prints WOMAN */
We have passed a copy of a pointer to ss. Now (in the function body) p is a name of this copy - local pointer variable. After that the function assigns another value (a pointer to the local array s) to this copy (parameter) variable. It has no relation to ss argument. The function f does nothing visible outside its scope!

May be it helps you to understand your snippets.

The last advice: try to get the best book about C - The C Programming Language by K&R.

Good luck!
 
Thank you ArkM,

I got the solution as follows
________________________________
#include <stdio.h>
#include<stdlib.h>
#include<string.h>

void newStrCpy(char* q, char* p);

int main (void)
{

char *src;
printf("\n----------------------------------------");
printf("\nPROGRAM TO COPY THE CONTENTS OF ONE STRING TO ANOTHER ");
printf("\n----------------------------------------");
printf("\n\n\t ENTER A STRING...: ");
scanf("%s",src);
char *dst =(char*) malloc(sizeof(*src));
newStrCpy(src,dst);
printf("\n\t copied string is..: %s\n",dst);

return 0;
}

void newStrCpy(char* q, char* p)
{

int i=0;

do{
*(p+i) = *(q+i);
i++;
}while(*(q+i)!='\0');

}

__________________________

I tried to use **p, but i couldn't figure it out . can you tell me how to do with using **p ,
what is the most efficient solution for this ?
 
Alas, there are tons of defects in every line of your code.
Please, read this excellent article written by Julienne Walker (All About Pointers):
then try to rewrite your code from the scratch...

The key point: try to understand the difference between arrays (real memory objects) and pointers (addresses of real memory objects).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top