segfault when passing array[] as argument
I'm new to using pointers and the like in C. I understand the concept, but I can't find where my problem is in the following small program. It's supposed to put an X in an array of Os and print out the users' choice, but it just segfaults. It's a small routine for a learning tic-tac-toe game I'm converting from an old Apple ][e book.
/* arraytest.h */ void cellput(char array[]) { short index=0; while(index<1 || index>9) { printf("\nEnter cell to put X (1-9): "); scanf("%d",&index); } array[--index]='X'; } /* arraytest.c */ #include<stdio.h> #include"arraytest.h" char array[]="OOOOOOOOO"; int main(void) { cellput(array); printf("\narray contains %s\n",array); return 0; } |
Does it segfault on any particular input? On startup? More detail, please. :)
|
From what I can determine with ddd, It segfaults when I step past the last } in main--when the arraytest exits. I watch array[] update from the call to cellput() and array gets printed fine. After arraytest exits I get the segmentation fault.
|
string literals are really of type const char * so trying to alter causes undefined behaviour.
|
That's odd, and sounds like it could be a library problem. how are you compiling this?
|
char array[] is NOT a literal, it's being allocated (statically) in the stack frame
|
Quote:
Then I see no obvious errors with the code other than an incorrect format specifier for scanf. For short int *, the proper format specifier is %hd (crank up the warning level when compiling to help you catch such slips). I tried the code with correct format specifier and compiled using gcc 4.0.0 and it ran without any crashes on my system. |
I'm compiling with gcc3.4 using the following command: gcc -g -o arraytest arraytest.c
When using ddd, this is what I get when I step past the end of main. Program received signal SIGSEGV, Segmentation fault. 0x00000000 in ?? () (gdb) I can't find anything wrong with the code (I'm by no means a guru). I've successfully compiled or built a whole system--LFS--and haven't run into any segfaults with system commands or daemons. This particular case leads me to believe I am missing something REALLY simple. I can compile other simple programs I've built for learning purposes and they exit fine. It only seems to be when I call a function with an array as an argument that I get segfaults. |
Ok, I'll try the %hd for short int. Do you know if that spec in the K&R C Programming Language book?
the %hd specifier? |
Amazing! It worked. That was just an h keeping my program from working. Been at this for 3 days. :)
Thanks a million. I am highlighting that section of my book and dog-earing it. |
Glad your problem went away. :)
|
Hrrm... I wonder if it was trying to write 4 bytes (even just 0x00 0x00 0x00 0x0n) to that two byte short... In a little-endian world, that would put a valid (non-zero) value in the short... and two zeros in a random position... such as, oh, the stack pointer? :)
|
All times are GMT -5. The time now is 06:40 PM. |