how to initialize the 2D pointer?

W

windstorm

for instance,I define a 2-D pointer:

char **input = NULL;

I want the "input" pointer to point to some string,and the number of
the string is dynamic.

when I got the first string,I was code like this:

*input = strtok( row,token );

because the function strtok return a string and the pointer "input" is
just point to this string,so I don't think it is need to be
initialized.There is no error when make but when I run the program
there is a error so-called "core dumped".

I think maybe it's because the pointer "input" wasn't initialized,so I
add something like this(although I don't think it is need):


*input = (char *)malloc( WORD_LENGTH * sizeof(char) );
*input = strtok( row,token );

But the error is still here,the same.

So,I wanna ask,how to initialize the 2D pointer?

I know that there is another way to use char*input[], but the number
the array can store is changeless.Even if I can use array to solve my
problem,but I will be disappointed if I cann't understand the question
I ask.
 
R

Richard Heathfield

windstorm said:
for instance,I define a 2-D pointer:

char **input = NULL;

That isn't a 2-D pointer. There is, in fact, no such thing as a 2-D pointer.
When you realise this, it will help you understand.
I want the "input" pointer to point to some string,and the number of
the string is dynamic.

when I got the first string,I was code like this:

*input = strtok( row,token );

Wait a minute. input points to NULL, so how can you assign to *input?

What you are missing is the first step, which is to get yourself space for a
bunch of pointers:

char **input = malloc(n * sizeof *input);
if(input != NULL)
{
/* you now have n pointers-to-char, input[0] through input[n - 1] */

for(i = 0; i < n; i++)
{
/* you can now point them somewhere - until you do, their values
are indeterminate.
*/
}
 
W

windstorm

windstorm said:
for instance,I define a 2-D pointer:
char **input = NULL;

That isn't a 2-D pointer. There is, in fact, no such thing as a 2-D pointer.
When you realise this, it will help you understand.
I want the "input" pointer to point to some string,and the number of
the string is dynamic.
when I got the first string,I was code like this:
*input = strtok( row,token );

Wait a minute. input points to NULL, so how can you assign to *input?

What you are missing is the first step, which is to get yourself space for a
bunch of pointers:

char **input = malloc(n * sizeof *input);
if(input != NULL)
{
/* you now have n pointers-to-char, input[0] through input[n - 1] */

for(i = 0; i < n; i++)
{
/* you can now point them somewhere - until you do, their values
are indeterminate.
*/
}

thanks a lot!
I got it.

By the way ,I am not familiar to the group's operation and I wrote
something just now and click the button "Reply to author" by false. I
am so sorry to Richard Heathfield, you can answer it or not. If you
decide to answer it, please post it to here.

thanks again.
 
D

Default User

windstorm said:
for instance,I define a 2-D pointer:

char **input = NULL;

I want the "input" pointer to point to some string,and the number of
the string is dynamic.

when I got the first string,I was code like this:

*input = strtok( row,token );

because the function strtok return a string and the pointer "input" is
just point to this string,so I don't think it is need to be
initialized.There is no error when make but when I run the program
there is a error so-called "core dumped".

I think maybe it's because the pointer "input" wasn't initialized,so I
add something like this(although I don't think it is need):


*input = (char *)malloc( WORD_LENGTH * sizeof(char) );
*input = strtok( row,token );

But the error is still here,the same.

So,I wanna ask,how to initialize the 2D pointer?

Richard told you pretty much everything, but review the FAQ entry:

<http://c-faq.com/aryptr/dynmuldimary.html>


Get used to using the FAQ, it has a lot of good information for
beginners.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top