Solved

dynamic alloc

Posted on 2000-05-01
4
294 Views
Last Modified: 2010-04-02
i want to declare a pointer pointing to an array of strings.the aim is to allocate memory for each string dynamically as and when required.the only data available is the length of each string, say fixed as 56 char.now tell me the c code.
0
Comment
Question by:vijay_a73
[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
4 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2768105
char **lpszItems = new char *[SIZEARRY];
for (int i = 0; i < SIZEARRY)
  lpszItem[i] = new char[SIZEELEMENT];

.....
for (int i = 0; i < SIZEARRY)
  delete  lpszItem[i] ;
delete [] lpszItem ;

Is it reply to you Q?

0
 
LVL 2

Expert Comment

by:gsatsan
ID: 2768179
There is no use knowing the length of each word. Rather the no. of words is important while dealing with pointers.
Here I assume 20 words with varying lengths.
This would do I guess:
 
 char **s;
 void func()
{int len;
 s = (char **)malloc(20);
 for(i = 0; i < 20; i++)
  {len = strlen(getword(i));
   s[i] = (char *) malloc(len*sizeof(char));
strcpy(getword[i],s[i]);
}
)
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2768434
things would be *much* simpler if you used a string class for this sort of thing.
0
 

Accepted Solution

by:
kishore_joshi earned 100 total points
ID: 2768485
Hai Vijay,
Check this out....

#include <stdio.h>
#include <malloc.h>

#define STRLEN 56

void main()
{

      int i, nos;   //  number of strings.
      
      char **ptr;

      printf("\n Enter the number of strings  u want to store : ");
      scanf("%d",&nos);
      
      
      ptr = (char **) malloc( nos * sizeof(char *));

      for( i = 0; i < nos ; i++) // allocate memory for each string and                                                      //  read it from consol
      {
            ptr[i] = (char *) malloc( STRLEN * sizeof(char));
            scanf("%s", ptr[i]);
      }

      
      for( i = 0; i < nos ; i++) // print all the strings....
      {
            puts(ptr[i]);

      }


      for( i = 0; i < nos ; i++)   // free the memory after use
      {
            free(ptr[i]);
      }

      free(ptr);

}
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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

730 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