Solved

dynamic alloc

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

815 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

8 Experts available now in Live!

Get 1:1 Help Now