?
Solved

newii cuestion?

Posted on 2003-03-27
2
Medium Priority
?
160 Views
Last Modified: 2010-04-01
why this raise an error?

#include <iostream>
#include <stdlib.h>


int main(int argc, char *argv[])
{
   
  char **array;
 
  int i;
  i = 0;
  array[i] = new char[31];
  array[i] = "test1";
  std::cout << array[i] << std::endl;
 
  system("PAUSE");    
  return 0;
}

using mingw
0
Comment
Question by:enmaca
[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
2 Comments
 
LVL 6

Accepted Solution

by:
GaryFx earned 200 total points
ID: 8220202
Before you can allocate new space for array[i], you must first allocate space for array.  If, for example, you want an array of ten character arrays, you could use something like:

array = new char*[10];
for (i=0; i<10; ++i) {
  array[i] = new char[31];
}

Of course, whenever you fixed length arrays like this, there's a question of whether or not they're big enough.

Gary
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 8220259
Your initialization of array is incorrect.
You can use something like:

  char * array[20];
  for (i=0;i<20;++i)
    array[i]= new char[20];
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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