Easy Pointer question

This is a discussion on Easy Pointer question within the C Programming forums, part of the General Programming Boards category; char string[30]; char *ptr_string; how do i copy ptr_string and put it into string? like this? Code: string = strdup(ptr_string); ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    Easy Pointer question

    char string[30];
    char *ptr_string;


    how do i copy ptr_string and put it into string?

    like this?
    Code:
    string = strdup(ptr_string);
    what about the other way?
    Code:
    ptr_string = strdup(string); ? or  is it
    ptr_string= (char *)malloc (strlen(string) * sizeof(char));

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    The way strdup() is described, you'd do something like:
    Code:
    ptr_string = strdup(string)
    but by the way you worded the question you want it assigned the other way around? There is no storage allocated for ptr_string yet. Only the array string[] has storage so far. Your guesses seem to be more like what I have. strdup does an implicit malloc for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 02:53 PM
  2. pointer question.... pointer theory?
    By panfilero in forum C Programming
    Replies: 6
    Last Post: 11-12-2005, 02:29 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 11:19 PM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 10:03 AM
  5. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 06:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21