Z
Zach
Wrote simple program to setup a char array of pointers and print each
one. It runs with the expected output however it then causes a
segmentation fault. Could someone take a lot at the code? My debugging
is below as well. Should I have malloc'd memory for each of the
strings? The examples I've studies so far from K&R and K&A didn't
indicate that was needed. I haven't learned malloc yet anyways
Zach
#include <stdio.h>
#include <stdlib.h>
#define NUMSTRINGS 3
int main (void)
{
char *strings[NUMSTRINGS] = {
"Testing 1",
"Testing 2",
"Testing 3"
};
int i;
for (i=0; i <= NUMSTRINGS; i++) {
printf("%s", strings);
printf("\n");
}
return(EXIT_SUCCESS);
}
zu22@netrek:~/src/testing$ gcc -g char-array2.c
zu22@netrek:~/src/testing$ gdb ./a.out
GNU gdb 6.4.90-debian
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i486-linux-gnu"...Using host libthread_db
library "/lib/libthread_db.so.1".
(gdb) run
Starting program: /home/zu22/src/testing/a.out
Testing 1
Testing 2
Testing 3
Program received signal SIGSEGV, Segmentation fault.
0x400959db in strlen () from /lib/libc.so.6
(gdb) bt
#0 0x400959db in strlen () from /lib/libc.so.6
#1 0x4006b327 in vfprintf () from /lib/libc.so.6
#2 0x40070cd3 in printf () from /lib/libc.so.6
#3 0x080483da in main () at char-array2.c:19
(gdb) quit
The program is running. Exit anyway? (y or n) y
one. It runs with the expected output however it then causes a
segmentation fault. Could someone take a lot at the code? My debugging
is below as well. Should I have malloc'd memory for each of the
strings? The examples I've studies so far from K&R and K&A didn't
indicate that was needed. I haven't learned malloc yet anyways
Zach
#include <stdio.h>
#include <stdlib.h>
#define NUMSTRINGS 3
int main (void)
{
char *strings[NUMSTRINGS] = {
"Testing 1",
"Testing 2",
"Testing 3"
};
int i;
for (i=0; i <= NUMSTRINGS; i++) {
printf("%s", strings);
printf("\n");
}
return(EXIT_SUCCESS);
}
zu22@netrek:~/src/testing$ gcc -g char-array2.c
zu22@netrek:~/src/testing$ gdb ./a.out
GNU gdb 6.4.90-debian
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i486-linux-gnu"...Using host libthread_db
library "/lib/libthread_db.so.1".
(gdb) run
Starting program: /home/zu22/src/testing/a.out
Testing 1
Testing 2
Testing 3
Program received signal SIGSEGV, Segmentation fault.
0x400959db in strlen () from /lib/libc.so.6
(gdb) bt
#0 0x400959db in strlen () from /lib/libc.so.6
#1 0x4006b327 in vfprintf () from /lib/libc.so.6
#2 0x40070cd3 in printf () from /lib/libc.so.6
#3 0x080483da in main () at char-array2.c:19
(gdb) quit
The program is running. Exit anyway? (y or n) y