I should remember this but I am rusty. I'm passing a char* [] to a function which will hold data that I have extracted using strtok. I've tested the output and it is doing what it is suppose to, at least it is inside of the function. As soon as I try to access the data inside of main I just get garbage. How do I pass the char* [] by reference? I thought that arrays were always passed by reference, well to be more specific that the pointer was copied but the array itself was by reference. I was trying something like this:

int main () {
     char* array [];
      function (array);     
}
void function (char* []){
     // fgets and other setup 
     array [0] = strtok (data, delimiters);
     // loop to max argument size or till out of arguments
         array[i] = strtok(NULL, delimiters);
}

Like I said I can check the array inside of the function and it is all there but I lose it when I get to main.

Thanks for the help

Try something like below

#include <stdio.h>
#include <string.h>

char data[] = "Hello, World!";
const char *delimiters = " ";  

void function (char* array [])
{
	array[0] = strtok (data, delimiters);

	fprintf(stdout, "first string->%s\n", array[0]);

	array[1] = strtok(NULL, delimiters);

	fprintf(stdout, "second string->%s\n", array[1]);
}

int main () 
{
	char* array[2];
	function(array);

	fprintf(stdout, "first string->%s\n", array[0]);

	fprintf(stdout, "second string->%s\n", array[1]);

	return 0;
}

here is how I approached it. Like I said it shows up in the function but not in main:

int getCommand (char* cmdAndParameters[]){
	int argNum = 0;
	bool foundArg = true;
	char* delimiters = " \t\n";
	char userRequest[CMDLEN];
	
	fgets (userRequest,sizeof(userRequest), stdin); 

	if ((cmdAndParameters[argNum] = strtok(userRequest, delimiters)) != NULL){		
		argNum++;

		for (; argNum < MAXARG && foundArg; argNum++){
			if ((cmdAndParameters[argNum] = strtok(NULL,delimiters)) == NULL){
				foundArg = false;
			}
				 
		}	
		
	}

	return argNum;
}

You realize this is wrong

char* array [];

This is taken from your original posting.

You realize this is wrong

char* array [];

This is taken from your original posting.

I have a constant value that is inside of the brackets (just forgot to type it in the example) if that is what you are talking about, otherwise please enlighten me.

I have a constant value that is inside of the brackets (just forgot to type it in the example) if that is what you are talking about, otherwise please enlighten me.

Maybe you can enlighten me as to how we(Daniweb) can diagnose a problem if the code is not complete.

Maybe you can enlighten me as to how we(Daniweb) can diagnose a problem if the code is not complete.

Sorry if I offended you there. Main right now just has a for loop for printing

#define MAXARG 5

int main {
     char* parameters [MAXARG];
     int totalArg = 0;
     
     totalArg = getCommand (parameters);

     for (int i = 0; i < totalArg; i++){
           printf("%s\n", parameters[i]);
     }
}

userRequest is local to getCommand, which means its memory disappears when control returns to main(). strtok() doesn't allocate new memory -- it just zero-terminates a token in the source string and returns a pointer to its first character. Therefore all the addresses in the parameters array are invalid when getCommand returns.

I would do something more like size_t getCommand(char *dest[], size_t size, char *source); so you can allocate the necessary memory in the caller and pass in a pointer to it. Implementation left as an exercise for the reader.

userRequest is local to getCommand, which means its memory disappears when control returns to main(). strtok() doesn't allocate new memory -- it just zero-terminates a token in the source string and returns a pointer to its first character. Therefore all the addresses in the parameters array are invalid when getCommand returns.

I would do something more like size_t getCommand(char *dest[], size_t size, char *source); so you can allocate the necessary memory in the caller and pass in a pointer to it. Implementation left as an exercise for the reader.

Thanks both of you for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.