LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-28-2005, 01:38 PM   #1
froboozle
LQ Newbie
 
Registered: Jun 2005
Location: USA
Distribution: LFS
Posts: 7

Rep: Reputation: 0
Question 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;
}
 
Old 06-28-2005, 01:53 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
Does it segfault on any particular input? On startup? More detail, please.
 
Old 06-28-2005, 02:16 PM   #3
froboozle
LQ Newbie
 
Registered: Jun 2005
Location: USA
Distribution: LFS
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 06-28-2005, 02:18 PM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
string literals are really of type const char * so trying to alter causes undefined behaviour.
 
Old 06-28-2005, 02:19 PM   #5
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
That's odd, and sounds like it could be a library problem. how are you compiling this?
 
Old 06-28-2005, 02:21 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
char array[] is NOT a literal, it's being allocated (statically) in the stack frame
 
Old 06-28-2005, 02:34 PM   #7
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.

Last edited by Hivemind; 06-28-2005 at 02:36 PM.
 
Old 06-28-2005, 02:35 PM   #8
froboozle
LQ Newbie
 
Registered: Jun 2005
Location: USA
Distribution: LFS
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 06-28-2005, 02:38 PM   #9
froboozle
LQ Newbie
 
Registered: Jun 2005
Location: USA
Distribution: LFS
Posts: 7

Original Poster
Rep: Reputation: 0
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?
 
Old 06-28-2005, 02:40 PM   #10
froboozle
LQ Newbie
 
Registered: Jun 2005
Location: USA
Distribution: LFS
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 06-28-2005, 02:57 PM   #11
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Glad your problem went away.
 
Old 06-28-2005, 03:06 PM   #12
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing int array to thread? Thinking Programming 2 09-21-2005 11:00 AM
passing an array from html-forms to php prabhatsoni Linux - Software 2 06-04-2005 12:11 AM
passing function pointer as argument worldmagic Programming 7 08-04-2004 03:33 PM
How does passing argument to main work Linh Programming 5 06-27-2003 01:08 PM
PHP Script argument passing error... lokee Linux - Software 5 04-24-2003 09:42 AM


All times are GMT -5. The time now is 07:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration