0

I'm trying to insert a string to a char* pointer but clearly not working.

I read that I may need to use malloc or something? But the other sites don't share them clearly.

char* token[1000];
gets(token);
printf("%s\n",*token);

Thanks

UPDATE:

Now I have to insert the pointer (which points to the string) to a function with the shown parameter.

int function(char *token[]);

char token[1000];
gets(token);
char*text=token;
printf("%s\n",text);

function(text);

Error of expected ‘char **’ but argument is of type ‘char *’

1 Answer 1

5

Your variable declaration is wrong, you have declared an array of 1000 character pointers, not 1000 characters. Remove the *.

Also never use gets(), always use fgets() instead since it's safer. There's nothing preventing the user from entering more than 1000 characters with gets(), which will overflow the buffer.

Sign up to request clarification or add additional context in comments.

1 Comment

Aditonally then you also have to remove the * in teh printf line as then *token isn't a string anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.