×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

string copy function
4

string copy function

string copy function

(OP)
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;
    
}

RE: string copy function

CODE

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

This allocates 1 byte.  You need

CODE

char *temp = (char*) malloc (strlen(q) + 1);

RE: string copy function

(OP)
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.  

RE: string copy function

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;
}



 

RE: string copy function

2
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!
 

RE: string copy function

(OP)
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 ?
 

RE: string copy function

Alas, there are tons of defects in every line of your code.
Please, read this excellent article written by Julienne Walker (All About Pointers):
http://eternallyconfuzzled.com/tuts/languages/jsw_tut_pointers.aspx
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).
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close