You are not logged in.

#1 2008-10-18 05:29:17

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

"makes pointer from integer without a cast" error in C

I am trying to create a variable-length array of strings, so an array of character arrays...

int num = 5;
char *questions = malloc(num * sizeof(char[100]));
memset(questions[1], 0, 100);
sprintf(questions[1], "hi");

If I try to compile this, I get two "makes pointer from integer without a cast" errors, one for the memset function and one for the sprintf function. Does anyone have any ideas? Thanks in advance.

Offline

#2 2008-10-18 05:42:55

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: "makes pointer from integer without a cast" error in C

You're creating an array of characters, not an array of strings. Also, questions[1] is the second element of the array, which may not be what you want.

char** questions = malloc(sizeof(char*[num]));
int i;
for (i = 0; i < num; i++) questions[i] = malloc(sizeof(char[100]));

Or somesuch. It's late, and I probably hid a big mistake in there.

Last edited by pauldonnelly (2008-10-18 05:48:53)

Offline

#3 2008-10-18 05:52:40

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: "makes pointer from integer without a cast" error in C

It seems to work; thanks!

Offline

Board footer

Powered by FluxBB