Public Group

# [C] char[][] problem

This topic is 3535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello. I'm working on the function, which gives me some errors:

void splitStr(char str[])
{
char temp[CHARS] = {0};

int spaces = countCharStr(str, ' '); /* count the spaces in the phrase */

error here -> char tempPhrase[spaces + 1][CHARS]; /* an array in the size of the number in words in the phrase */

int occDot = occurence(str, '.', 1) - str; /* get the postion of the last character in the phrase
- it is always a full-stop. */

int i = 0, j;
char *p;
for(; *str; ++str, ++i)
{
int occSpace = occurence(str, ' ', i + 1) - str; /* get the position of space # i */
//printf("i = %d, occSpace = %d\n", i, occSpace);

if(!i) /* i = 0, first loop iteration */
for(j = 0; j <= occSpace; ++j) /* <= and not < to include  the space in the word */
temp[j] = str[j];

}

printf("temp = %s\n", temp);
}


The errors are: error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0 How can I fix that? (I'm not sure but that's one of the issus C++0x is going to solve). I don't want to wait for it to be released :p Thanks in advance.

##### Share on other sites
Quote:
 Original post by sheep19char tempPhrase[spaces + 1][CHARS]

Well, you can't declare arrays of variable length in C89 or C++. Only C99 allows that.

Oh and what is CHARS? Can you post the line of code where you define that name?

Quote:
 Original post by sheep19I'm not sure but that's one of the issus C++0x is going to solve

If you're writing C++, you shouldn't be using char arrays anyways. Use std::string and/or std::vector.

##### Share on other sites
try

    char *tempPhrase = new char[spaces + 1][CHARS];

and dont forget to delete it after.

##### Share on other sites
Quote:
Original post by DevFred
Quote:
 Original post by sheep19char tempPhrase[spaces + 1][CHARS]

Well, you can't declare arrays of variable length in C89 or C++. Only C99 allows that.

Oh and what is CHARS? Can you post the line of code where you define that name?

CHARS is
#define CHARS 100
I know it's not a good name. :p

I'm using Visual C++ 2008EE but I'm doing the program in C to become familiar with C-strings.

##### Share on other sites
Quote:
 Original post by mcmuzzletry char *tempPhrase = new char[spaces + 1][CHARS]; and dont forget to delete it after.

Well I'm programming in C so I can't do that.

##### Share on other sites
Quote:
Original post by sheep19
Quote:
 Original post by mcmuzzletry char *tempPhrase = new char[spaces + 1][CHARS]; and dont forget to delete it after.

Well I'm programming in C so I can't do that.

  char* tempPhrase = (char*)malloc(sizeof(char)*(spaces + 1)*(CHARS));

  free tempPhrase;

##### Share on other sites
Strange, I still get errors:

void splitStr(char str[]){	char temp[CHARS] = {0};	int spaces = countCharStr(str, ' '); /* count the spaces in the phrase */	char *tempPhrase = (char*)malloc(sizeof(char)*(spaces + 1)*(CHARS)); /* an array in the size of the number in words in the phrase */	int occDot = occurence(str, '.', 1) - str; /* get the postion of the last character in the phrase										    - it is always a full-stop. */	int i = 0, j;	char *p;	for(; *str; ++str, ++i)	{		int occSpace = occurence(str, ' ', i + 1) - str; /* get the position of space # i */		//printf("i = %d, occSpace = %d\n", i, occSpace);				if(!i) /* i = 0, first loop iteration */			for(j = 0; j <= occSpace; ++j) /* <= and not < to include  the space in the word */				temp[j] = str[j];	}	printf("temp = %s\n", temp);	free tempPhrase;}

warning C4013: 'system' undefined; assuming extern returning intwarning C4013: warning 'malloc' undefined; assuming extern returning int
error C2065: 'free' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'tempPhrase'

Why is that? Maybe I should use C++ and not C, but keep the C-strings. Will it make a difference?

##### Share on other sites
You need to include <stdlib.h> to use malloc and free. Also, free is a function so you need free(whatever); not free whatever;

##### Share on other sites
Quote:
 Original post by EasilyConfusedYou need to include to use malloc and free. Also, free is a function so you need free(whatever); not free whatever;

I just figured it out. But malloc and free work without including stdlib. (At least for me) :/

##### Share on other sites
Quote:
 Original post by c0uchm0nsterchar* tempPhrase = (char*)malloc(sizeof(char)*(spaces + 1)*(CHARS));

There is no need to cast the result of malloc in C, as void* can be implicitly converted to any other pointer type. It is required however if you want to write "upwards" (C++) compatible code.

• 38
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631362
• Total Posts
2999577
×