Click to See Complete Forum and Search --> : Character Array problem...


Handyman
04-14-2003, 05:57 PM
Yep, this is something I probably should have learned long ago, but alas the problem hasn't reared it's ugly head until now :) I have the following code:



#include <stdio.h>
#include <string.h>
#include "stack.h"
#include "mcsfcn.h"

int main(void)
{
char* temp_eqn;

// Prompt for and read in equation from user //

printf("MCS Reverse Polish Notation Calculator\n\n");
printf("Please enter your equation: ");
scanf("%s",&temp_eqn);
printf("\n\n");

for ( int i = 0 ; i < length_of(temp_eqn) ; i++ )
printf("%c ",temp_eqn[i]);

return 0;
}



Nothing in the stack.h file has been called yet, but the mcsfcn.h contains the length_of() function I used in my for loop. The function is declared as follows.



int length_of(char* x)
{
for ( int i = 0 ; i != '/0' ; i++ );

return i;
};



So my problem occurs after the user has entered their string and the for loop is entered. It's supposed to just spit the string back out at me (printed char by char in the loop). However, all I get is about 20 lines of random ASCII characters and then the program ends.

So where abouts do I seem to be going wrong? Is my Length_of() function to blame? Since the loops seems to last for far longer than it should. Or is it in how I'm accessing my character array? Any help would be much appriciated :)

PS: I'm well known for making the simpliest/dumbest mistakes, so feel free to laugh at my inferior programming arse :) hehe

Thanks in advance!!

Malone
04-14-2003, 06:59 PM
First off, use a temporary variable to store the length_of() your string. As it is you call length_of(char* x) every iteration of the for loop, which can be costly for long strings. Just a tip. :)

Second of all, your length_of(char* x) function is pretty flawed. Does it even compile? It looks like it just iterates forever, because it doesn't compare any characters. Try this:
int i=0;
while(x[i] != '\0') {i++;}
return i+1;

I haven't done any C coding in awhile, but I think that will work. It's also a good idea to allocate some memory for "char* temp_eqn" before passing it into any functions like scanf or length_of.

Handyman
04-14-2003, 07:26 PM
Originally posted by Malone
First off, use a temporary variable to store the length_of() your string. As it is you call length_of(char* x) every iteration of the for loop, which can be costly for long strings. Just a tip. :)

Second of all, your length_of(char* x) function is pretty flawed. Does it even compile? It looks like it just iterates forever, because it doesn't compare any characters. Try this:
int i=0;
while(x[i] != '\0') {i++;}
return i+1;

I haven't done any C coding in awhile, but I think that will work. It's also a good idea to allocate some memory for "char* temp_eqn" before passing it into any functions like scanf or length_of.

Ah... nifty, thanks for the tip on the for loop. Your advice shall be taken :)

Hehe.. like I said I make stupid mistakes. With my length of fixed I still get messed up output. This time only two lines of random ASCII chars now... at least it's a step in the right direction :)

Thanks! :)

Malone
04-14-2003, 11:27 PM
No problem:)

The random output indicates to me that there is just random data in the area of memory accessed by your pointer. I can't remember if an uninitialized pointer simply points to null or if it can point to any memory address. But try this to specifically set aside memory for a 256 character array in your main() function.

char* temp_eqn;
temp_eqn = (char *) malloc(256*sizeof(char));
// so now a block of 256 bytes has been set aside to hold your string
// you can make it bigger or smaller than 256 if you like, i just
// picked it out of thin air

now you can scanf("%s",&temp_eqn);