Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...It's extraordinarily refreshing to see truly expert advice without having to wade through hipper than thou attitude..."

Geography

Where in the world do Tek-Tips members come from?

string copy functionHelpful Member!(4) 

manichandra (TechnicalUser)
6 Feb 12 18:03
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;
    
}
xwb (Programmer)
7 Feb 12 1:30

CODE

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

This allocates 1 byte.  You need

CODE

char *temp = (char*) malloc (strlen(q) + 1);
manichandra (TechnicalUser)
7 Feb 12 2:18
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.  
Helpful Member!  xwb (Programmer)
7 Feb 12 3:19
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;
}



 
Helpful Member!(2)  ArkM (IS/IT--Management)
7 Feb 12 5:24
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!
 
manichandra (TechnicalUser)
7 Feb 12 22:48
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 ?
 
Helpful Member!  ArkM (IS/IT--Management)
8 Feb 12 6:12
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).
 

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!

Back To Forum

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