1

My problem should be fairly simple, but I can't seem to find a place where this is done in the exact manner I am doing it, or at least nowhere that I can adapt to this.

The idea here is to declare a char pointer, then using realloc, expand it into an array that I can then assign pointers that point to the starting character of an array.

From what I've read before, I should be able to just access it as if it were a two-dimensional array, but when I try the following code, I get an error: Invalid indirection in function main when I try to use printf.

#include <stdio.h>

char *testArr;

unsigned char arr1[5] = {10, 20, 30, 40, 50};
unsigned char arr2[7] = {1, 2, 3, 4, 5, 6, 7};
unsigned char arr3[3] = {50, 150, 200};

int main(void){

realloc(testArr, sizeof(testArr) + sizeof(char *));
realloc(testArr, sizeof(testArr) + sizeof(char *));
testArr[0] = &arr1;
testArr[1] = &arr2;
testArr[2] = &arr3;

printf("%i", *testArr[0][3]);

getchar();

return 0;
}

I've tried a few things, including removing &'s from the assignment of the values into the pointer array, but I am honestly at a loss here. Perhaps someone with better C experience can help me here. Thanks.

8
  • Why do you call realloc() 3 times with the same arguments? Commented Feb 10, 2018 at 3:49
  • Just quick and dirty coding to see if it works. This isn't final code. In hindsight I have forgotten to remove one of them, as it should really only need it twice. Commented Feb 10, 2018 at 3:51
  • You should only need it once. Commented Feb 10, 2018 at 3:51
  • The idea in the future is to implement it into a loop. Either way, it's just somewhat inefficient programming that I do not believe affects the final product. Thanks for looking and helping btw. Commented Feb 10, 2018 at 3:52
  • But it's the same size each time, so you're not changing the size. What's the point of calling it more than once with the same size? Do you think sizeof(testArr) is going to change? It's the size of a pointer. Commented Feb 10, 2018 at 3:53

2 Answers 2

2

The argument to malloc or realloc should be the number of elements in the array multiplied by the size of each element, you shouldn't be adding. And you need to assign the result of the function to a variable.

testarr = realloc(testarr, 3 * sizeof(char*);

Since you want it to be an array of pointers, you need to declare it as:

char **testarr;
Sign up to request clarification or add additional context in comments.

2 Comments

As it turns out that is the issue. I think it might have been due to me not using two asterisks. Looking that up it does appear that's needed. Many thanks.
I'd check this as the correct answer, but it's saying I need to wait a few minutes.
1

You want to hold the unsigned char*-s. You will have to use unsigned char** in this setup.

unsigned char **testArr;

In main() the correct way to use realloc would be (this return value checking is needed).

char **t = realloc( testArr, sizeof(*t)*3);
if(!t) { perror("realloc"); exit(1);}
testArr = t;

Now you can simply do this (earlier what you did is assigning char (*)[] which even if you try in this case compiler would complain due to type mismatch)

textArr[0]= arr1;

Printing that would be

printf("%i", testArr[0][3]);

Comments

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.