LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   segfault when passing array[] as argument (https://www.linuxquestions.org/questions/programming-9/segfault-when-passing-array%5B%5D-as-argument-338083/)

froboozle 06-28-2005 01:38 PM

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;
}

Matir 06-28-2005 01:53 PM

Does it segfault on any particular input? On startup? More detail, please. :)

froboozle 06-28-2005 02:16 PM

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.

Hivemind 06-28-2005 02:18 PM

string literals are really of type const char * so trying to alter causes undefined behaviour.

Matir 06-28-2005 02:19 PM

That's odd, and sounds like it could be a library problem. how are you compiling this?

Matir 06-28-2005 02:21 PM

char array[] is NOT a literal, it's being allocated (statically) in the stack frame

Hivemind 06-28-2005 02:34 PM

Quote:

Originally posted by Matir
char array[] is NOT a literal, it's being allocated (statically) in the stack frame
Of course you are correct, Matir. That's the proper way to initialize a string that's not const. I had a temporary mind slip there.
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.

froboozle 06-28-2005 02:35 PM

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.

froboozle 06-28-2005 02:38 PM

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?

froboozle 06-28-2005 02:40 PM

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.

Hivemind 06-28-2005 02:57 PM

Glad your problem went away. :)

Matir 06-28-2005 03:06 PM

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.