0

I am trying to reassign a pointer to a 3-dimensional array.

include <stdio.h>

int main()
{
int type;

char database_of_names[3][50][16];
char database_of_suffixes[10][50][16];

char (*databaseP)[][50][16];

...

if(type == 0)
    (*databaseP)[][50][16] = &database_of_names; // Runtime error
else if(type ==1)
    (*databaseP)[][50][16] = &database_of_suffixes; // Runtime error

...

return 0;
}

I can initialize the pointer at the point of declaration like this:

char (*databaseP)[][50][16] = &database_of_names; // works

but I want to be able to switch the assignment. How can I do this by changing to above code?

Thanks

3
  • 2
    You appear to be in the need of reading a good beginner C book, especially one that explains pointers well. Commented Dec 15, 2013 at 17:25
  • @H2CO3; Partially agreed. This question confused me at first glance. Commented Dec 15, 2013 at 17:27
  • 1
    @haccks But then you figured it out. "Confuses me at first glance" is not the same as "I have no idea what I am doing here" :P Commented Dec 15, 2013 at 17:37

4 Answers 4

2

Change

(*databaseP)[][50][16] = &database_of_names;  

to

databaseP = &database_of_names;
Sign up to request clarification or add additional context in comments.

Comments

1

I am trying to reassign a pointer to a 3-dimensional array.

Then reassign the pointer, not the random memory space it points to (since it's uninitialized). You don't need to dereference it.

databaseP = &database_of_names;

is more than enough.

Comments

1

With

(*databaseP)[][50][16] = &database_of_names

you first dereference the pointer, then try to index it. Don't do that, just assign the pointer:

databaseP = &database_of_names;

Comments

1

Although the change in the other answers will make the code you have shown compile, what you probably want is to change the definition of databaseP to:

char (*databaseP)[50][16];

and the assignments of databaseP to :

if (type == 0)
    databaseP = database_of_names;
else if (type ==1)
    databaseP = database_of_suffixes;

The reason for this is that, although the changes suggested in the other answers would indeed set databaseP to point to database_of_names or database_of_suffixes, it would be a pointer to those arrays. This means you would have to use it with an extra dereference operator, as in:

… (*databaseP)[i][j][k] …

However, in C, it is usually more convenient to point the first element of an array rather than the array itself. In this case, the first element of the three-dimensional arrays is a two-dimensional array. The code I showed above defines databaseP to be a pointer to a two-dimensional array and assigns it correctly for that. The result is you can use databaseP more simply, as in:

… databaseP[i][j][k] …

Incidentally, the messages you were getting on the statements shown with “Runtime error” in your question were actually compile-time errors. They occurred when your program was compiled, not when it was run.

1 Comment

Thanks for that explanation. Yes that is a cleaner code. Thanks!(p.s. why are there so many geek snobs in SO, why the -1 vote. Yes I have already researched this question. BTW if everyone researched all their questions %100 there would really be no need for SO huh.)

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.