Passing a char * into char array

This is a discussion on Passing a char * into char array within the C Programming forums, part of the General Programming Boards category; I have a function that returns a char *. I have another function that wants a char array as a ...

  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,185
    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
    21,326
    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 10:49 AM. Reason: char* -> char *temp
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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, 04:16 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04: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

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