Thread: strcpy small confusion

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59

    strcpy small confusion

    Hi all, I want to copy a string to a char array using strcpy function. I just want to ask whether the following is correct
    Consider:
    Code:
     char a[10];
     strcpy(a,"Hello");
    Or should i use:
    Code:
     char *ptr,a[10];
     ptr=a;
     strcpy(ptr,"Hello");
    I have mostly seen the later approach being used, I want to know what is the harm in the first approach?
    Any suggestions would be helpful!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The only difference between the two is that the second one makes use of an extra useless pointer, ptr. I think what you have seen before was dynamic vs static memory allocation for an array. Your first example would be static allocation whereas something like:

    Code:
    char *ptr;
    ptr = malloc(10);
    strcpy(ptr,"Hello");
    would be a dynamic allocation of memory followed by the strcpy call.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by gaurav_13191 View Post
    I have mostly seen the later approach being used,
    Keep looking. It's far more normative to use the first approach, unless there is some subsequent use for "ptr" (such as iterating thru the string in "a").

    I want to know what is the harm in the first approach?
    There isn't any.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gaurav_13191 View Post
    Hi all, I want to copy a string to a char array using strcpy function. I just want to ask whether the following is correct
    Consider:
    Code:
     char a[10];
     strcpy(a,"Hello");
    Or should i use:
    Code:
     char *ptr,a[10];
     ptr=a;
     strcpy(ptr,"Hello");
    I have mostly seen the later approach being used, I want to know what is the harm in the first approach?
    Any suggestions would be helpful!
    The first approach is "By Design" , the second is "By Confusion"...
    Seriously, why would you use extra variables if you don't need them?

  5. #5
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    Thanks for your replies.. even I didn't see any point in taking an extra variable.. but just wanted to confirm that the first approach was correct as I haven't used it

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gaurav_13191 View Post
    Thanks for your replies.. even I didn't see any point in taking an extra variable.. but just wanted to confirm that the first approach was correct as I haven't used it
    FWIW... Test stuff. If it works, use it. If it doesn't... don't.

    You be amazed at the number of little "test proggies" I make to discover the best way to do things that end up in larger projects.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    Yes, even I keep trying running random programs to clear my doubts.. a great way to learn the language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small programming confusion in "C"...
    By GajananDon in forum C Programming
    Replies: 1
    Last Post: 06-20-2010, 10:11 PM
  2. Replies: 1
    Last Post: 06-19-2010, 07:42 AM
  3. strcpy
    By Abda92 in forum C Programming
    Replies: 7
    Last Post: 09-13-2006, 10:05 AM
  4. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM
  5. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM