|
-
Character Array problem...
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:
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.
Code:
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!!
| Processor | Intel Pentium 4 @ 2800 MHz | Motherboard | ABIT IC7-Max3 | Memory | 2 GB Corsair XMS PC3200 DDR | Graphics | ATI Radeon 9800 Pro | OS/Prog HDD | W.D. Raptor 36GB (10k) SATA | Storage HDD | Seagate 160GB (7200) SATA | Burner | Lite-On 52x24x52 CDRW | DVD-ROM | Lite-On 16x DVD | Sound Card | Philips Acoustic Edge | Speakers | Klipsch ProMedia 2.1 | Flash Media | Lian-Li Flash Media Bay | Display | Dual NEC 22" Flat CRT | Case/PSU | Lian-Li PC-65B w/ Antec 430W | O.S. | MS Windows XP Pro | Printer | Epson Stylus Photo 2200
-
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.
Last edited by Malone; 04-14-2003 at 06:59 PM.
AMD AthlonXP 2600+ Thoroughbred B @ 200x10.5
Shuttle AN35N nForce2 Ultra 400
2x512MB Kingston PC3200 (3-3-3)
ATI Radeon 9600 Pro
40GB WD ATA-100 8MB cache
Creative 12X DVD Drive
Memorex 52X CD-RW
Running Windows XP Pro
-
Last edited by Handyman; 04-14-2003 at 07:27 PM.
| Processor | Intel Pentium 4 @ 2800 MHz | Motherboard | ABIT IC7-Max3 | Memory | 2 GB Corsair XMS PC3200 DDR | Graphics | ATI Radeon 9800 Pro | OS/Prog HDD | W.D. Raptor 36GB (10k) SATA | Storage HDD | Seagate 160GB (7200) SATA | Burner | Lite-On 52x24x52 CDRW | DVD-ROM | Lite-On 16x DVD | Sound Card | Philips Acoustic Edge | Speakers | Klipsch ProMedia 2.1 | Flash Media | Lian-Li Flash Media Bay | Display | Dual NEC 22" Flat CRT | Case/PSU | Lian-Li PC-65B w/ Antec 430W | O.S. | MS Windows XP Pro | Printer | Epson Stylus Photo 2200
-
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);
Last edited by Malone; 04-15-2003 at 01:33 AM.
AMD AthlonXP 2600+ Thoroughbred B @ 200x10.5
Shuttle AN35N nForce2 Ultra 400
2x512MB Kingston PC3200 (3-3-3)
ATI Radeon 9600 Pro
40GB WD ATA-100 8MB cache
Creative 12X DVD Drive
Memorex 52X CD-RW
Running Windows XP Pro
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|