dynamic array of variable-length strings!

I believe that everything is fine except that I can think of the condition can be inserted inside the parentheses ..

#include <iostream>
using namespace std;

int main()
{
int i=0, k=0;
char *string_n, **matrix, temp;

string_n=(char*)malloc(sizeof(char));
matrix=(char**)malloc(sizeof(char*));

cout<<"Insert a name: ";

while() //<<<<<<<<<<---------------------
{
cout<<"Insert a name: ";
while((temp=getchar())!='\n')
{
string_n[i]=temp;
string_n=(char*)realloc(string_n,sizeof(char));
i++;
}
string_n[i]='\0';
matrix[k]=stringa;
matrix=(char**)realloc(matrix,sizeof(char*));
}

system("pause");
}
Last edited on
Do not use malloc/realloc/calloc/free in a C++ program.

You're allocating arrays of length 1 byte and indexing past the end of that 1 byte array.
kbw wrote:
Do not use malloc/realloc/calloc/free in a C++ program.

Not an issue, methinks.

kbw wrote:
You're allocating arrays of length 1 byte and indexing past the end of that 1 byte array.

+ 1

You are trying to create a robust solution to getting variable-length input with C-strings. You should use a function for that. Here's a simple one I wrote a while ago to demonstrate the tricky issues for doing that:
http://www.cplusplus.com/forum/beginner/4174/#msg18271

Good luck!
Topic archived. No new replies allowed.