Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > C bus error w/ pointers

C bus error w/ pointers
Thread Tools
Fresh-Faced Recruit
Join Date: Nov 2002
Status: Offline
Reply With Quote
Nov 17, 2002, 09:06 PM
 
Yet another question from a beginner reading a book.

What is wrong with this code:

char *p[][2] = {
…string table stuff…
};

int main()
{
int i;
char str[80];

printf("Enter apple name: ");
fgets(apple, 80, stdin);

/*search array */
for(i=0; *p[i][0]; i++){
if(!strcmp(apple, *p[i][0]))
printf("%s and %s.", str, *p[i][1]);
}
}

I copied it just like it says in my book, and when it gets the the for loop it quits with a bus error. It doesn't seem to look for a bad memory address. Please help me! (again ).
     
Mac Enthusiast
Join Date: Sep 2000
Location: Cupertino, CA
Status: Offline
Reply With Quote
Nov 18, 2002, 07:30 PM
 
Originally posted by Spiffster:
Yet another question from a beginner reading a book.

What is wrong with this code:

Code:
char *p[][2] = { …string table stuff… }; int main() { int i; char str[80]; printf("Enter apple name: "); fgets(apple, 80, stdin); /*search array */ for(i=0; *p[i][0]; i++){ if(!strcmp(apple, *p[i][0])) printf("%s and %s.", str, *p[i][1]); } }
I copied it just like it says in my book, and when it gets the the for loop it quits with a bus error. It doesn't seem to look for a bad memory address. Please help me! (again ).
The simple answer is the code is wrong, and any book that uses it as an example is not a good book to be learning from.

The longer answer is that its using arrays in a way that is basically pointer arithmatic. And its wrong. You could fix those bugs by changing the for loop to:

Code:
/*search array */ for(i=0; p[i][0]; i++){ if(!strcmp(str, p[i][0])) printf("%s and %s.", str, p[i][1]); } }
And you also have to make sure that the last entry in the array is a NULL. Even that will probably not get you what you want, since the fgets will have a "\n" at the end of i, and chances are the strings in your array do not.

So if you want a fully working example you get:

Code:
#include <stdio.h> char *p[][2] = { "apple\n", "test", NULL, NULL }; int main() { int i; char str[80]; printf("Enter apple name: "); fgets(str, 80, stdin); /*search array */ for(i=0; p[i][0]; i++){ if(!strcmp(str, p[i][0])) printf("%s and %s.", str, p[i][1]); } } [tofueater:~] local% ./a.out Enter apple name: apple apple and test.[tofueater:~] local%
Of course you probably want to strip the return from the end of string, but the way this code is arranged the only consistent way would be:

Code:
#include <stdio.h> char *p[][2] = { "apple", "test", NULL, NULL }; int main() { int i; char str[80]; printf("Enter apple name: "); fgets(str, 80, stdin); str[strlen(str)-1] = 0; /*search array */ for(i=0; p[i][0]; i++){ if(!strcmp(str, p[i][0])) printf("%s and %s.", str, p[i][1]); } } [tofueater:~] local% [tofueater:~] local% ./a.out Enter apple name: apple apple and test.[tofueater:~] local%
Again, this is not good fix, its just consistent with the exisiting code. I am somewhat curious what the book is, and what the chapter is about. Unless this from a chapter on pointer aliasing or something of the kin I would say the book is just totally rotten, and even then its really questionable code.

Louis
Louis Gerbarg
Darwin Developer
These are my views, and not the views of my employer.
     
Fresh-Faced Recruit
Join Date: Nov 2002
Status: Offline
Reply With Quote
Nov 18, 2002, 09:37 PM
 
Actually, it was my fault. I tried to rewrite the example. It originally used array indexes, and i tried to rewrite it using pointer artihmatic. I guess i fscked up pretty baddly 8^).
     
Mac Enthusiast
Join Date: Sep 2000
Location: Cupertino, CA
Status: Offline
Reply With Quote
Nov 21, 2002, 04:29 AM
 
Originally posted by Spiffster:
Actually, it was my fault. I tried to rewrite the example. It originally used array indexes, and i tried to rewrite it using pointer artihmatic. I guess i fscked up pretty baddly 8^).
Its not bad for someones first time trying it, I thought it was actually a published example from a book. I would not have been quite so harsh had I realized that.

The thing you want to remember is that in C arrays are just syntactic sugar for pointer math. So in your above code:

a[i] is equivalent to (char *)(*(a + i) ) and
a[i][j] is equivalent to (char *)(*(a + i + j))

In fact that causes one of those fun oddities of C:

a[i][j] == a[j][i] == i[a][j] == j[i][a] .....

Louis
Louis Gerbarg
Darwin Developer
These are my views, and not the views of my employer.
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:21 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2