Thread: Easy Pointer question

  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,834
    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, 01: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, 10:19 PM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM
  5. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM