0

Good day to everyone,

there's some kind of big deal that I cannot figure out. I create a multidimensional array of pointers:

char *listA[5000][2];

In particular condition, I want that specific strings are saved inside this array. These are a normal string, in a simple variable, and another contained inside an array of strings.

 listA[j][0]=strMix;
 listA[j][1]=ingredients[i];
 j++;

j, of course, is the row, that is increased for every adding.

The result must be an array that, for every row, contains two columns, one whit an ingredient, and another with the relative strMix.

The problem is that when I try to read this multidimensional array:

printf( "%s", listA[0][1]); // the ingredient

is always correct, but:

printf( "%s", listA[0][0]); // the strMix code

is always incorrect; precisely it reads the last strMix read, for every row.

I tried to change the order of the columns, and with my big surprise, the problem is always for the strMix, and never for the ingredients[i] string.

strMix column is correct only if I write it inside listA, and immediately read it. Of course, I'd say.

For example:

printf("Current: %s vs Previously: %s",lista[j][0], lista[j-1][0]);

they are the same, for every j, equal to the last strMix read.

If you have any ideas, something about memory or multidimensional array of pointers that I simply are missing, I'd appreciate your advises.

Thank you for the time, in every case. fdt.

4
  • 1
    Everything you've shown there seems reasonable - show us the code where you initialize this array. That's probably where your trouble is. Commented May 5, 2013 at 13:40
  • This is a common problem. Often the problem is that a new string isn't being allocated for each item read. Commented May 5, 2013 at 13:43
  • And show us where you're setting the pointer strMix, or the contents of the array pointed to by it. Commented May 5, 2013 at 13:44
  • Tnx Carl Norum, Vaughn Cato and Andy Thomas-Cramer. strMix is a string manipulated by a function. Commented May 5, 2013 at 14:08

2 Answers 2

2

You're not saving strings in this array -- you're saving pointers.

This statement copies an address, not a string:

 listA[j][0]=strMix;

If you want to copy the string, you can either:

  • Use an char array that stores the strings. For some strings, this may use too much memory. For others, it may offer not enough.
  • Use a char* array that stores addresses to the strings, and allocate separate memory for each referenced string.

Regardless, to copy the strings, prefer strncpy() to the unsafe strcpy().

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

3 Comments

thanks for the answer. I tried to use the address, to point strMix by a pointer and pass it to listA, but the problem is the same. strMix is actually an array of char.
When you use strmix by itself, it's value is an address, whether you've declared it char strmix[] or char* strmix. If you the code where you're declaring it and setting it, the fix should be fairly simple.
I get it! I totally get it. The solution was inspired by your help. The problem was indeed the declaration of strMix (char strMix[20]). 'ingredients' is (ingredients[TOT][TOT]: 2D-array of char). I create an array of char also for strMix, manipulate (a lot!) the code to use it correctly, but now it works. Thanks a lot.
1

Is it C or C++? Maybe instead of using the assignment:

listA[j][0]=strMix;

you should use strcpy function?

strcpy (listA[j][0], strMix);

1 Comment

Thank you for the answer. I tried with strcpy and strncpy, but the program crashes. It's C.

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.