ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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)
while(index<1 || index>9)
printf("\nEnter cell to put X (1-9): ");
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 ?? ()
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.
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?