Return an array of pointer, return type?

You have a problem there. You're declaring the array as a local variable. This means it will die (be realeased) in the end of the block.

If you want to return it you need to dynamically allocate it (and remember to free it later).

Up vote -2 down vote favorite share g+ share fb share tw.

I have a function.. char ** getCommand() { char *commandArrayBUFSIZ; //declaring an array of char pointers // I m doing things to array return commandArray; } I am getting an error saying "conflicting types". What is the type of commandArray? It is a pointer to a pointer right?

I tried char *getcommand()...that didn't work either. I am calling this function in main so I have delcared.. main() { char *commands; commands = getCommand(); //this didn't work // So I redeclared... char *commandsBUFSIZ; commands = getCommand(); //this didn't work either. } Whats going on?

I have never worked w/ array of pointers before... someone simplify the problem...and give me some kind of hint pls.. EDIT Ok thanks for the suggestions...didn't work... I am pasting the code...suggested changes reflected..getting same error, saying the getCommand has conflicting error types. Char **getCommand() { int command_num; char inputBUFSIZ; char **commandArray; command_num = 0; commandArray = (char**) malloc (BUFSIZ * sizeof(char)); fgets(input,sizeof(input),stdin); commandArraycommand_num = strtok(input, " "); /*breaks a string of commands into tokens*/ while (commandArraycommand_num! = NULL) { printf ("%s\n", commandArraycommand_num); command_num++; commandArraycommand_num = strtok (NULL, " "); } commandArraycommand_num = NULL; /*very imp - adds null in the last position*/ return commandArray; } c arrays pointers char link|improve this question edited Jun 20 '11 at 5:07Chris12k21434 asked Jun 19 '11 at 21:14Naz122 0% accept rate.

2 Please post a minimal, complete example that demonstrate the problem, the whole problem and nothing but the problem. – Kerrek SB Jun 19 '11 at 21:15 you might try posting your revision as a comment for one of the answers -- what didn't work? – Chris Jun 20 '11 at 5:08 Wow!

Your returning dead! – Akito Jun 20 '11 at 6:25 @Chris: the problem is fixed now. Thank you.

– Naz Jun 21 '11 at 21:27.

You have a problem there. You're declaring the array as a local variable. This means it will die (be realeased) in the end of the block.

If you want to return it you need to dynamically allocate it (and remember to free it later) char** getCommand() { char **commandArray = (char **)malloc(BUFSIZ* sizeof(char*)); // I m doing things to array return commandArray; }.

The input array - which commandArray points to - is also on the stack. – Dipstick Jun 20 '11 at 0:15.

The problem is commandArray is an array of pointers which is stored in getCommand()'s stack frame, so this is, technically speaking, undefined behavior. The solution is to change commandArray to a char ** and use malloc() to allocate the whole array.

Following are the 2 changes needed: Should not return reference to local variable (which is stored on function stack), instead allocate memory from heap and returns its reference. Receiving pointer in main should also be of type char ** char **getCommand(){ char **command = (char **) malloc(N*BUFSIZE); //Do something to command array return command; } int main(){ char **commands; commands = getCommand(); }.

With char **cmdArray you have a pointer to pointers. But the pointer does not have a valid value. Use malloc to reserve some space for a few pointers and assign that value to the pointer cmdArray = malloc(20 * sizeof (char*)); Now, cmdArray points to an area of 20 pointers.

But none of those 20 pointers have a valid value. You need to allocate space for each of those 20 pointers for (k = 0; k.

Dynamically allocating the commandArray as suggested by others is insufficient as the allocated array being returned contains pointers into the input array and the input array is also on the stack so goes out of scope when the function getCommand() returns. To see if this is the problem change: char inputBUFSIZ; To static char inputBUFSIZ.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions