Solved

Multiple Indirection

Posted on 1998-05-16
2
168 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
Comment Utility
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
Comment Utility
Alright, that is what I thought, but just wanted to make sure, thankyou!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now