ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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);
}
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.
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.
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.
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.