Hi, in the declaration of IP_name:

int InputNumber;
        ...
        // function that gets the InputNumber
        ...
	char IP_name[InputNumber];

I have this error:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'IP_name' : unknown size

How can I solve this problem?
Thanks

allocate the array with either malloc() or new, depending on whether you are writing a c or c++ program.

That's it :)
thanks

allocate the array with either malloc() or new, depending on whether you are writing a c or c++ program.

Hi, I've done a new like this:

char *IP_name;
	     IP_name= new char [InputNumber];

             for (int j=0; j<InputNumber; j++)
	    {
		
		// Read data back from input file: 
		fscanf( fINPUT, "%s", &IP_name[j]);

		printf("%s\n", &IP_name[j]);
	    }

            delete []IP_name;

It compiles without errors, but it crashes... :(

Where is fINPUT anywhere in your code ? It would be really better for the others to understand if you post the entire code or the module so that we can be of some help.

Hoping to hear from you.
Bye.

Regards,
~s.o.s~

Where is fINPUT anywhere in your code ? It would be really better for the others to understand if you post the entire code or the module so that we can be of some help.

Hoping to hear from you.
Bye.

Regards,
~s.o.s~

Hi, here it is:

// Open for read (will fail if file input.txt does not exist)
	if( (fINPUT  = fopen( "my_log_input.txt", "r" )) == NULL )
	{
		printf( "The file 'my_log_input.txt' was not opened\n" );
		exit(1);
	}

regards

>>fscanf( fINPUT, "%s", &IP_name[j]);

IP_name is a simple charater array, not an array of strings. If you want to store two or more strings in IP_name (for example "Joe" and "Harray") then you need to make IP_name an array of InputNumber number of strings. Something like this. But if you are writing a c++ program you should use c++ vector and string classes to avoid the messy memory allocation stuff and associated error-prone problems.

int InputNumber = 5;
char ** IP_name;
char buffer[255]; // for file i/o
IP_name = new char*[InputNumber];
for (int j=0; j<InputNumber; j++)
{
		
   // Read data back from input file: 
   fscanf( fINPUT, "%s", buffer);
   IP_name[j] = new char[strlen(buffer)+1];
   strcpy(IP_name[j],buffer);
}

// don't forget to deallocate all that memory when done with it.

Hi I've done like this and it works:

char **IP_name;
	IP_name = new char* [InputNumber];
	for (int i=0; i<InputNumber; i++)
	{
		IP_name[i] = new char[20];
                fscanf( fINPUT, "%s", &IP_name[j]);
		printf("%s\n", &IP_name[j]);
	}

The only problem is to free the memory with delete... I tried with:

for (i=0; i<InputNumber; i++)
	{
		delete IP_name[i];
	}
	delete IP_name;

Unsucessful... :(

Hi I've done like this and it works:

char **IP_name;
	IP_name = new char* [InputNumber];
	for (int i=0; i<InputNumber; i++)
	{
		IP_name[i] = new char[20];
                fscanf( fINPUT, "%s", &IP_name[j]);
		printf("%s\n", &IP_name[j]);
	}

The only problem is to free the memory with delete... I tried with:

for (i=0; i<InputNumber; i++)
	{
		delete IP_name[i];
	}
	delete IP_name;

Unsucessful... :(

you forgot to use delete[] instead of delete. Remember: new[] requires delete[] -- they go in pairs.

you forgot to use delete[] instead of delete. Remember: new[] requires delete[] -- they go in pairs.

like this?

for (i=0; i<InputNumber; i++)
	{
		delete []IP_name[i];
	}
	delete []IP_name;

Also crashes...

are the input strings longer than 19 characters? there might be something else going on in your program that is trashing the memory and pointers.

are the input strings longer than 19 characters? there might be something else going on in your program that is trashing the memory and pointers.

The input strings are IP names like this: "193.231.035.201"...

fscanf( fINPUT, "%s", &IP_name[j]);
		printf("%s\n", &IP_name[j]);

remove the & symbol, the compiler is passing a pointer to a pointer, which is not what you want it to do.

fscanf( fINPUT, "%s", IP_name[j]);
		printf("%s\n", IP_name[j]);

> fscanf( fINPUT, "%s", &IP_name[j]);
> printf("%s\n", &IP_name[j]);
You're still using & where you shouldn't (on both of them).

Gah - beaten

> fscanf( fINPUT, "%s", &IP_name[j]);
> printf("%s\n", &IP_name[j]);
You're still using & where you shouldn't (on both of them).

Gah - beaten

That's it, Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.