Solved

Multiple Indirection

Posted on 1998-05-16
2
202 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 recursion in the C programming language.

726 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