Solved

Multiple Indirection

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

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…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

920 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

14 Experts available now in Live!

Get 1:1 Help Now