?
Solved

dynamic mem all: array of strings

Posted on 2003-02-20
8
Medium Priority
?
975 Views
Last Modified: 2008-02-01
I need to dynamically allocate an array of string I seem to be having trouble with it ex code:

char * str;
str=(char *)calloc(sizeof(char*),100);

I think it may be that I need to cast a return of a different type; it compiles and runs fine but when I print out a stored string nothing is printed out.
0
Comment
Question by:skeid21
[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
8 Comments
 
LVL 3

Expert Comment

by:cmaryus
ID: 7992014
You should do something like this:

char * str;
str=(char *)calloc(100, sizeof(char));

- where the first parameter is the number of elements in the array
- the second parameter size of an element. The elements are char's and NOT pointer to char! The variable str is a pointer to char.

In this way you allocate an array with 100 char's. Using calloc also initialize all the elements to 0. That is also the diference between malloc and calloc, malloc does the same thing but don't initialize elements to 0.
0
 
LVL 3

Expert Comment

by:cmaryus
ID: 7992015
Also don't forget to release the memory once you're done with str:
free(str);
0
 
LVL 1

Expert Comment

by:rainbowsix
ID: 7992081
#include <stdio.h>
#include <malloc.h>

void main( void )
{
   char *sbuffer;

   sbuffer = (char *)calloc( 100, sizeof( char ) );
   if( sbuffer != NULL )
      printf( "Allocated 100 characters\n" );
   else
      printf( "Can't allocate memory\n" );
   free( sbuffer );
}
0
Technology Partners: 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!

 
LVL 3

Accepted Solution

by:
cincin77 earned 400 total points
ID: 7992178
skeid21 wants to allocate an array of strings not characters.
You should do:


char * * str;
str=(char * *)calloc(sizeof(char*),100);
for (int i=0;i<100;i++)
   str[i]=(char *)calloc(sizeof(char),YYY)

where YYY is the length of each string.

hope this helps.
0
 
LVL 2

Expert Comment

by:ellesd
ID: 7993396
Why are you using calloc() and not malloc()?  The malloc() system call is great for char strings and buffers:

str = (char*)malloc(100);

Usually calloc() is used for other data types and if making an array of strings (which are arrays of chars).

If you must use calloc():

str = (char)calloc(100, sizeof(char));

Your call.
0
 
LVL 5

Expert Comment

by:Kocil
ID: 7998277
str=(char *)calloc(sizeof(char),100);
or
str=(char *)calloc(100,sizeof(char));

will work.

str=(char *)calloc(sizeof(char),100);
will do no harm since sizeof(char*) > sizeof(char).

So I think it's your print function that has problem.

Check if you use
printf("%s", str);

and NOT

printf("%s", &str);

0
 

Author Comment

by:skeid21
ID: 8005783
Thanks for the great answer
I have found one other way to deal with this prob:

char * str[];

str=(char**)calloc(sizeof(char*),array demension);

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

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