Solved

dynamic alloc

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now