Thread: Passing a char * into char array

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Passing a char * into char array

    I have a function that returns a char *. I have another function that wants a char array as a parameter.
    Code:
    char * func1() {
      char *temp = malloc(sizeof(char) * 10);
      strcpy(temp, "hello");
      return temp;
    }
    
    void func2(char temp[10]) {
      yadda yadda;
    }
    
    int main() {
      printf("%s", func2(func1()));
    }
    Will that work as expected?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No. func2 does not return anything, so its non-existent return value cannot be used in main, because, well, it's non-existent.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    bah! That was dumb...

    Code:
    char * func1() {
      char *temp = malloc(sizeof(char) * 10);
      strcpy(temp, "hello");
      return temp;
    }
    
    void func2(char temp[10]) {
      printf("%s", temp);
    }
    
    int main() {
      func2(func1());
    }
    how about that? I get a suspicious pointer conversion warning...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bladactania
    how about that?
    You should store the return value of func1() so that you can use free() on it after you are done. Other than that yes, it should work (assuming that malloc() does not return a null pointer), but note that the char temp[10] parameter is effectively the same as char *temp, and that sizeof(char) is always equal to 1.

    Quote Originally Posted by Bladactania
    I get a suspicious pointer conversion warning.
    Did you forget to include the three relevant headers, by any chance? It certainly looks like that in your example.
    Last edited by laserlight; 02-20-2009 at 11:49 AM. Reason: char* -> char *temp
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That should be legitimate; func2 expects a pointer to 10 characters, and you are passing it a pointer to 10 characters (and since the memory is malloc'ed, it's still a valid pointer). The compiler might not be able to figure that out, I guess.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM
  5. passing char where array of chars required
    By larry in forum C++ Programming
    Replies: 4
    Last Post: 09-25-2001, 01:24 PM