473,386 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to initialize the 2D pointer?

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.

Feb 2 '07 #1
3 5772
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.
*/
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 2 '07 #2
On Feb 2, 5:51 pm, Richard Heathfield <r...@see.sig.invalidwrote:
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.
*/
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
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.

Feb 2 '07 #3
windstorm wrote:
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

Feb 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

74
by: Peter | last post by:
Hi, So many times, I have seen compile warning: "you used a char* without initilize it", probably on the code like this: ------------ char* ptr; func(..., ptr); ----------
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
7
by: Andi.Martin | last post by:
Hi, how is it possible, to only initialize parts of a structure. Example: typedef struct{ int a, b; ... (huge lot of members); double x,y;
19
by: moxm | last post by:
I have a statement declares a globle variable like this : char *pname = NULL; Then I used splint to check the code, I got errors: err.c:8:15: Global pname initialized to null value: pname =...
22
by: 海风 | last post by:
How to initialize a pointer in c++, Mostly, I use null, for example, char * szName = null; However, if i compile it without including afxdisp.h , .net compiler tell me that the identifier is not...
10
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
4
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I...
18
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: ...
3
by: Hank stalica | last post by:
So I have this class with a private data member: const Floor* const destFloor; I need to initialize f, which I'm trying to do in the constructor initializer list: Rider::Rider(const...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.