?
Solved

Dynamic String Arrays & Passing them to Functions in C

Posted on 2003-02-22
4
Medium Priority
?
249 Views
Last Modified: 2010-04-15
I was wondering how to go about dynamically allocating an array of strings, filling them in with values, and then sending them to a function to be processed.  And if there is a difference in manipulating the dynamic string array once passed to a function, I'd be curious to see how to do that too, please.
0
Comment
Question by:TJspliff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
gj62 earned 300 total points
ID: 7998987
In C, an array of strings is really an array of pointers to strings (or more appropriately, pointers to arrays of char), and to allocate them you can do the following:

This will allocate storage for 10 strings, each having 100 chars...

char **str;
int i;

str = (char **)malloc(sizeof(char*),10);

for (i = 0; i < 10; i++)
  str[i] = (char *)malloc(sizeof(char),100);

You can fill the strings by using str[i], where i = 0 through 9...

strcpy(str[3],"This is the 4th string...");

You can have a funcion definition as:

somestringfunc(char **str)
{
  int x;

  /* the following lines will print all strings - for the 4th string, it would print:

The 4 string is This is the 4th string...

*/
  for (x = 0; x < 10; x++)
    printf("The %d string is %s\n", x+1, str[x]);
 
}

and pass your array of strings in a call simply as:

somstringfunc(str);


0
 
LVL 5

Expert Comment

by:Kocil
ID: 7999077
0
 

Author Comment

by:TJspliff
ID: 7999117
Thank you gj62! That is really good, clean code that I can understand! :)

Kocil, so it seems, I'm such a noob.  Thank you both!
0
 

Author Comment

by:TJspliff
ID: 7999123
As i commented in my question, thank you again, the code and explaination is very clear and helpfull!
0

Featured Post

Industry Leaders: 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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

764 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