Jump to content
  • Advertisement
Sign in to follow this  
sheep19

[C] char[][] problem

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by sheep19
char 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 sheep19
I'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 this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by sheep19
char 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 this post


Link to post
Share on other sites
Quote:
Original post by mcmuzzle
try


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 this post


Link to post
Share on other sites
Quote:
Original post by sheep19
Quote:
Original post by mcmuzzle
try


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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused
You need to include <stdlib.h> 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 this post


Link to post
Share on other sites
Quote:
Original post by c0uchm0nster
char* 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!