Solved

Multiple Indirection

Posted on 1998-05-16
2
198 Views
Last Modified: 2006-11-17
Hi,
     I'm writing a program and I want to set up a variabel to hold dynamic length strings(char *), and I'm thinking
that I should use char ** for that variable? But then here comes my real question, since that will hold a variable ammount of those char * strings(accessed via char **foo[0] char **foo[1] etc.) how do I allocate dynamic memory for that? foo = (char **) malloc(sizeof(char) * 500) (Obviously in the real code I'd have something more dynamic than a flat 500, but).

       thanks, Jason
0
Comment
Question by:jwilcox
2 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 70 total points
ID: 1250820
If you wish to declare a variable to hold one dynamic length
string it would indeed be a char *; e.g.

char *foo;

foo=malloc(500);

foo is pointer to character, and it now points at a block of
memory containing 500 bytes. Or to put another way, it is a pointer
to an array of characters. Or foo could be a string (if the
memory will be null terminated.

To get a variable number of these you could indeed declare:

char **foo;

To allocate memory for this, you would need two steps:

foo=malloc(10*sizeof(char *));

This has allocated a block of memory that will hold 10 pointers
to character. To access it you can write foo[0] (or its
equivalent *foo). This references the first pointer to character
in the array. foo[1] would access the second one etc.

To actually allocate a string you now write:

foo[0]=malloc(20);

This makes pointer to character in foo[0] point to a block of
memory that contains twenty bytes. It can, of course, be
considered an array, or a string. foo[1], foo[2], foo[3] etc.
can be initialized the same way.

So you see, in this case, where you have a dynamic number of
dynamic sized objects, char **foo really is pointer to pointer to
character. The code will literally follow the pointer in foo to
find something that is a pointer to another block of memory.

0
 
LVL 1

Author Comment

by:jwilcox
ID: 1250821
Alright, that is what I thought, but just wanted to make sure, thankyou!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question