 |
 |
C bus error w/ pointers
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Nov 2002
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|