?
Solved

dynamic mem all: array of strings

Posted on 2003-02-20
8
Medium Priority
?
980 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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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!

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

616 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