0

I have an array inside a struct like so:

    typedef struct mystruct{
        const char *myarr[30];
    } mystruct;

I need to grow this array later in the program to 60 elements by creating a new array, duplicating the content, and then changing myarr to point to the new array.

I have tried the following:

const char newtable[n];
s->*myarr = newtable;

But gcc complains:

error: incompatible types in assignment

Any ideas as to the right way to accomplish this?

5
  • 2
    You can't just start off with the array size at 60? Commented Oct 10, 2011 at 3:43
  • No, the 30 and 60 were just simple examples. In reality, the program is like a hashtable. Commented Oct 10, 2011 at 3:46
  • It's a bit unclear what you're trying to do: const char* arr[30] is an array of 30 char* but then you try to change it to point to an array of n char. Commented Oct 10, 2011 at 3:52
  • You have ->* notation - but that is a C++ operator, not a C operator. Typo? Commented Oct 10, 2011 at 4:33
  • Ah, then I am using the wrong operator. I'm trying to have an array of strings that holds 30 elements, and then change it to larger later. Commented Oct 10, 2011 at 5:16

4 Answers 4

3

Assuming that you indeed want your array to contain char *s, not chars, you should define your structure like this:

typedef struct {
    const char **myarr;
    /* I assume you actually have more members here */
} mystruct;

and initialize it like this:

mystruct s;
s.myarr = (const char **) malloc(30 * sizeof(const char *));
if (!s.myarr) { /* handle out-of-memory condition somehow */ }

Then you can later extend it with realloc():

const char **tmp = (const char **) realloc(s.myarr, 60 * sizeof(const char *));
if (tmp) s.myarr = tmp;
else { /* handle out-of-memory condition somehow */ }

(Note that, if realloc() returns NULL, the original value of s.myarr will still be valid.)

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

2 Comments

I'm having a little trouble using this solution: Compiler is complaining: error: request for member ‘myarr’ in something not a structure or union. NOTE: I am trying to do typedef struct stringtable{} instead of just typedef struct{} because I need to use -> syntax rather than . syntax. Struct vs Object?
In C, foo->bar is just a shorthand for (*foo).bar. You should use . when you have the struct itself as a variable, and -> when you only have a pointer to the struct.
1

Don't allocate it as an array inside the struct. Just leave it as a pointer:

const char *myarr;

Then this'll work:

const char newtable[n];
s->myarr = newtable;

You can still use it like an array, e.g.

char c = s->myarr[20];

1 Comment

Thanks for responding Graham, please take a look at my response to lostyzd
0

You declare an array of pointer, not a pointer point to a array.

typedef struct { // no need mystruct here
    const char *myarr;
} mystruct;

const char newtable[60];
s->myarr = newtable;

But const char *myarr is different from const char (*myarr)[30], and you didn't actually create a new array by const char newtable[60];, you maybe need malloc instead.

5 Comments

Thanks for the response, I'm attempting this right now but I'm getting an error when trying to add elements to the array like so: s->myarr[arrindex] = str. Error: assignment of read-only location
@user987042 My bad, I forget to tell you that const char means readonly, so you should declare char newtable[60] or use char *newtable = malloc(60)
Thank you sir, this has solved my array sizing issues. Unfortunately, this change broke the following line: if(strcmp(s->myarr[arrindex], "") == 0). This line results in a segmentation fault now. Is this because strcmp takes a const variable, and I am no longer declaring it as const?
Actually, I'm unable to do even the following: printf("%s\n", s->myarr[10]); Any ideas?
@user987042 It seems that u r using an "char **" as the answer u marked, so please update your question with more details, I don't understand what you are trying to do.
0

So growing memory region at runtime should really involve dynamic memory allocation. I'd suggest something like this:

typedef mystruct {
   char *data;
} mystruct;

[...]

char *ptr = realloc(s->data, 60); // that will copy your previous data over
if (ptr != NULL) {
   s->data = ptr;
}

I don't think it has to be much more complicated than that really... especially you should avoid declaring a 60 elements array statically in the .data/.rodata section...

Hope it helps,

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.