0

Following other inquiries I found on this site, I'm trying to print an array of pointers to characters (strings), by using a **double pointer as follows:

char *input=malloc(128), *color[7]={0}, **colors=malloc(8); 

fgets(input, 128, stdin);
sscanf(input, "%s%s%s%s%s%s", &color[0], &color[1], &color[2], &color[3], &color[4], &color[5]);
color[6]=NULL;  
colors=color;

while(*colors)
printf("%s ", colors++);

The output I receive from the above seems to be choking every input string down to 4 bytes and then overflowing if it is more than four, because if I type: "black red gold white green blue", for instance, I receive: "blacred red goldwhitgreeblue goldwhitgreeblue whitgreeblue greeblue blue." Very frustrating.

I tried dereferencing colors++, like I saw in other examples, but this causes the program to crash. Ultimately, the printf is just for debugging. This is part of a char ** (void) function I'm trying to create to assign what it is I'm trying to print to a local variable of type char **. How can I fix this?

4
  • malloc() is not magic... Commented Nov 24, 2013 at 7:16
  • Okay, cool. Not that I ever thought it was. Commented Nov 24, 2013 at 7:21
  • Well, you were using it as if it was, so... Commented Nov 24, 2013 at 7:28
  • I was using it to allocate memory. By the way, which part of my question was unclear or showed no research effort? I've been on this site and others searching for similar issues for the last three hours. If that was indeed you, I don't appreciate snobby downvotes, simply for being a 3-month old C programmer. Commented Nov 24, 2013 at 7:41

1 Answer 1

2

When you do

char **colors = malloc(8);

you allocate only 8 bytes to the variable colors. Not enough to store six strings. If you want eight strings, why not simply create an array of eight strings?

char *colors[8] = { NULL };

The above declaration declares colors as an array of eight pointers to char, all pointers initialized to NULL.

If you desperately need to allocate of the heap, then you do e.g.

char **colors = calloc(8, sizeof(char *));

I use calloc to make sure that the allocated memory is zero-initialized (i.e. all pointers will be NULL).


Furthermore, sscanf does not automatically allocate space for the strings it scans, you have to do it manually. The easiest is probably to use the 'm' modifier to the format code, like

sscanf(input, "%ms %ms %ms %ms %ms %ms",
       &color[0], &color[1], &color[2], &color[3], &color[4], &color[5]);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. The only reason I'm using the double pointer is so I can return it in a function. From what I understand, I can't return an array, correct? This is my dilemma. I did try allocating more memory to **colors, but still does the same thing. The reason I had allocated such a low amount was because I thought I only needed a handful of addresses, one to each element of *color[].
@mosdellg Then check out my line in the answer with the calloc call. And it's correct that you can't return a pointer to a local array from a function. And remember that the size provided to malloc is the number of bytes, and that a pointer is usually four (on 32-bit platforms) or eight (on 64-bit platforms) bytes.

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.