Solved

dynamic alloc

Posted on 2000-05-01
4
295 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

717 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