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
Solved

dynamic alloc

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 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.

860 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