Newbie has problem with array of structures

J

Jobs'R'Us

Hello,

Newbie has problem with array of structures using gcc on Linux Redhat 9...

The code:

struct { char mystring[50]; int myint; } mystruct;

int otherintvariable = 10;

for(int i=0;i < 20;i++)
{
mystruct.mystring[0] = 'a'; //trying to assign one character in a specific place
mystruct.myint = otherintvariable;
}

Both lines inside the for loop give compile errors with gcc:
"subscripted value is neither array nor pointer"

Please dumb it way down!

Thanks!
Kirst
 
T

Tom St Denis

Jobs'R'Us said:
Hello,

Newbie has problem with array of structures using gcc on Linux Redhat 9...

The code:

struct { char mystring[50]; int myint; } mystruct;

What type is "mystruct"
int otherintvariable = 10;

for(int i=0;i < 20;i++)
{
mystruct.mystring[0] = 'a'; //trying to assign one character in a


Are you sure mystruct is an array or pointer?

And babbabooyie babbaboyie howard stern rocks!

Tom
 
E

Emmanuel Delahaye

Jobs'R'Us wrote on 12/08/04 :
Hello,

Newbie has problem with array of structures using gcc on Linux Redhat 9...

The code:

struct { char mystring[50]; int myint; } mystruct;

int otherintvariable = 10;

for(int i=0;i < 20;i++)
{
mystruct.mystring[0] = 'a'; //trying to assign one character in a
specific place mystruct.myint = otherintvariable;
}

Both lines inside the for loop give compile errors with gcc:
"subscripted value is neither array nor pointer"


Sure. You defined 'mystruct' as single object, not like an array. You
probably want

struct
{
char mystring[50];
int myint;
}
mystruct[20];

instead. (Note that a good intendation makes things clearer and easier
to read and maintain...)
 
E

Emmanuel Delahaye

Tom St Denis wrote on 12/08/04 :
struct { char mystring[50]; int myint; } mystruct;

What type is "mystruct"

Doesn't have to have a type (actually, you meant a tag).

The type of 'mystruct' is 'struct { char mystring[50]; int myint; }'
 
T

Tom St Denis

Emmanuel said:
Tom St Denis wrote on 12/08/04 :
struct { char mystring[50]; int myint; } mystruct;

What type is "mystruct"

Doesn't have to have a type (actually, you meant a tag).

The type of 'mystruct' is 'struct { char mystring[50]; int myint; }'

I mean it's a single object not a pointer or array.

Right up there with what I said in the REST OF MY REPLY.

Tom
 
J

Jobs'R'Us

You
probably want

struct
{
char mystring[50];
int myint;
}
mystruct[20];

instead.

Hello,

You are right, the above does compile. However the program displays a
segmentation fault when run. Will have to debug and/or try again with
a smaller program. Does anyone have a web reference, code sample,
tutorial that uses this syntax?

Thanks much for your help,
Kirst
 
J

Jobs'R'Us

Hello again,

I have reproduced my problem with less code. The following compiles
but throws a "Segmentation fault" when run. Am I missing a linker
library?

Online references would be most welcome.

//mystruct.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>

// compiled with command:
// gcc mystruct.c -omystruct

// my program will use args
int main(int argc, char *argv[])
{
struct
{ char myword[50];
int myintone;
int myinttwo;
} mystruct[5000];

char mystring[] = "mystring";

int i;
for(i=0;i < 5000;i++)
{
strcpy(mystruct.myword,mystring);
mystruct.myintone = i;
mystruct.myinttwo = i + 5;
}

printf("myword at 1222 is %s",mystruct[1222].myword);
printf("myintone at 1222 is %s",mystruct[1222].myintone);
printf("myinttwo at 1222 is %s",mystruct[1222].myinttwo);

return 0;
}
//end

Thank you and best regards,
Kirst
 
P

pete

Jobs'R'Us said:
Hello,

Newbie has problem with array of structures using gcc on Linux Redhat 9...

The code:

struct { char mystring[50]; int myint; } mystruct;

int otherintvariable = 10;

for(int i=0;i < 20;i++)
{
mystruct.mystring[0] = 'a'; //trying to assign one character in a specific place
mystruct.myint = otherintvariable;
}

Both lines inside the for loop give compile errors with gcc:
"subscripted value is neither array nor pointer"

Please dumb it way down!



#include <stdio.h>

int main(void)
{
struct {
char mystring[50];
int myint;
} mystruct[20];
int i;

for (i = 0; 20 > i; ++i) {
mystruct.mystring[0] = 'a';
mystruct.myint = i;
}
for (i = 0; 20 > i; ++i) {
printf("%c %d\n", mystruct.mystring[0], mystruct.myint);
}
return 0;
}
 
E

Emmanuel Delahaye

Jobs'R'Us wrote on 12/08/04 :
#include <stdio.h>
#include <string.h>
#include <ctype.h>

// compiled with command:
// gcc mystruct.c -omystruct

// my program will use args
int main(int argc, char *argv[])
{
struct
{ char myword[50];
int myintone;
int myinttwo;
} mystruct[5000];

char mystring[] = "mystring";

int i;
for(i=0;i < 5000;i++)
{
strcpy(mystruct.myword,mystring);
mystruct.myintone = i;
mystruct.myinttwo = i + 5;
}

printf("myword at 1222 is %s",mystruct[1222].myword);
printf("myintone at 1222 is %s",mystruct[1222].myintone);
printf("myinttwo at 1222 is %s",mystruct[1222].myinttwo);

return 0;
}



main.c: In function `main':
main.c:28: warning: format argument is not a pointer (arg 2)
main.c:29: warning: format argument is not a pointer (arg 2)
main.c:9: warning: unused parameter `argc'
main.c:9: warning: unused parameter `argv' gcc.exe -D__DEBUG__ main.o
-o
 
J

Jens.Toerring

Jobs'R'Us said:
Hello again,
I have reproduced my problem with less code. The following compiles
but throws a "Segmentation fault" when run. Am I missing a linker
library?
Online references would be most welcome.
//mystruct.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// compiled with command:
// gcc mystruct.c -omystruct
// my program will use args
int main(int argc, char *argv[])
{
struct
{ char myword[50];
int myintone;
int myinttwo;
} mystruct[5000];

Beside the problems others have mentioned another one could be here.
For this you need more that 250000 bytes of memory, which typically
will end up on the stack and which might simply be more than is
possible on the system you're using. And, as far as I can see, the
C89 standard doesn't guarantee more than 32767 bytes for a single
object anyway. Try that with a smaller array (or allocate the
memory you need instead) and the program shouldn't segfault anymore.

Regards, Jens
 
C

CBFalconer

Jobs'R'Us said:
I have reproduced my problem with less code. The following compiles
but throws a "Segmentation fault" when run. Am I missing a linker
library?

Online references would be most welcome.

//mystruct.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>

// compiled with command:
// gcc mystruct.c -omystruct

// my program will use args
int main(int argc, char *argv[])
{
struct
{ char myword[50];
int myintone;
int myinttwo;
} mystruct[5000];

char mystring[] = "mystring";

int i;
for(i=0;i < 5000;i++)
{
strcpy(mystruct.myword,mystring);
mystruct.myintone = i;
mystruct.myinttwo = i + 5;
}

printf("myword at 1222 is %s",mystruct[1222].myword);
printf("myintone at 1222 is %s",mystruct[1222].myintone);

^^
here, and next line, should use %d. %s is for strings, %d for
integers. Lying to variadic functions about the type of parameter
passed cause undefined behavior. gcc -W -Wall would have
complained about this. Also splint. For code that is on-topic
here you should always use gcc -W -Wall -ansi -pedantic. -ansi
can be replaced by -std=C99. All assuming you are using gcc.
printf("myinttwo at 1222 is %s",mystruct[1222].myinttwo);

return 0;
}
//end
 
C

Christopher Benson-Manica

Emmanuel Delahaye said:
instead. (Note that a good intendation makes things clearer and easier
^^^^^^^^^^^

The road to hell is paved with good indenations ;)
 
G

G. S. Hayes

I have reproduced my problem with less code. The following compiles
but throws a "Segmentation fault" when run. Am I missing a linker
library?

No, but a couple of ideas...
// gcc mystruct.c -omystruct

Slightly offtopic, but similar facilities are available in most real
life C implementations:
Investigate using the warning options of whatever compiler you're
using, and try to make sure that you understand what each warning
means. For instance, when compiled with "gcc -Wall mystruct.c -o
mystruct", I get the following:

mystruct.c: In function `main':
mystruct.c:29: warning: format argument is not a pointer (arg 2)
mystruct.c:30: warning: format argument is not a pointer (arg 2)

Which are very handy tips about one problem in your code. Using
"-Wall -W" will give you even more warnings (it's worth writing code
that cleanly compiles with -Wall -W, in my opinion).
int main(int argc, char *argv[])
{
struct
{ char myword[50];
int myintone;
int myinttwo;
} mystruct[5000];

Implementation detail: On many platforms, putting big objects on the
stack can overflow the stack and cause segfaults. Consider using
dynamic allocation for large arrays. I don't think that's hitting you
in this case, but it can be hard to track down when it happens.
printf("myintone at 1222 is %s",mystruct[1222].myintone);
printf("myinttwo at 1222 is %s",mystruct[1222].myinttwo);

These are lines 29 and 30 (which the compiler warned about).

What does printf do with "%s"? What type are mystruct[n].myintone and
..myinttwo?

Sumner
 
A

Arvind Varma Kalidindi

Hi,
The second and third printf's are using %s for printing
integers....that is bad on the programmer's part ( I don't see any
valid reason as to why you would want to do that)....Could you please
tell where exactly are you getting the segmentation fault...I will try
to run the program when I get to my lab sometime later in the day.

Regards,
Arvind.
 
J

Jobs'R'Us

Hello and thanks all you fine and picky folks!

Arvind, yes of course I made a typo this morning writing and compiling
the simple program before rushing off to work, it should of course be
%d and not %s. The Seg fault does not provide a line number, which is
why I went to the trouble of writing a simpler program to narrow down
my issue.

Emmanuel, I think you're barking up the wrong tree, the actual program
does have parameters, I believe the Seg fault has nothing to do with
that, but I will try compiling without arguments in that main()
declaration to see if it makes a difference just because I've been
wrong before.

I will try the std=C99 Chuck suggested. That looks promising.

Jens suggested it was memory, but I did think of that and tried a much
smaller number in the actual program, still got the Seg fault. Will
try recompiling the simple program with a much smaller number to be
sure.

I agree with G.S.Hayes that it's worth writing code that cleanly
compiles with -Wall -W, I am simply doubting that I know all that is
required about arrays of structures, specifically about subtler syntax
for gcc.

Pete has suggested some strange syntax, I will try compiling that too.

So do we all agree that the following should compile and run?

//mystruct.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
struct
{
char myword[50];
int myintone;
int myinttwo;
} mystruct[20];

char mystring[] = "mystring";

int i;
for(i=0;i < 20;i++)
{
strcpy(mystruct.myword,mystring);
mystruct.myintone = i;
mystruct.myinttwo = i + 5;
}

printf("myword at 12 is %s",mystruct[12].myword);
printf("myintone at 12 is %d",mystruct[12].myintone);
printf("myinttwo at 12 is %d",mystruct[12].myinttwo);

return 0;
}
//end

And if it was needed, what would the syntax to do dynamic allocation
for large arrays look like exactly in this case?


Thanks for your patience,
Kirst
 
E

Emmanuel Delahaye

Christopher Benson-Manica wrote on 12/08/04 :
^^^^^^^^^^^

The road to hell is paved with good indenations ;)

If you mean that less typos make the text easier to read, I agree!

Of course, I meant 'indentation'.
 
P

pete

Jobs'R'Us said:
printf("myinttwo at 12 is %d",mystruct[12].myinttwo);

return 0;
}
//end

Whether or not the last line of a stream needs to be newline
terminated, is implementation defined, so it's better
to have the last line to the standard output stream be
newline terminated for portability reasons.
And if it was needed, what would the syntax to do dynamic allocation
for large arrays look like exactly in this case?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NMEMB 20

int main(void)
{
struct {
char myword[50];
int myintone;
int myinttwo;
} *mystruct;
char mystring[] = "mystring";
int i;

mystruct = malloc(NMEMB * sizeof *mystruct);
if (mystruct == NULL) {
fputs("I'm tired\n", stderr);
exit(EXIT_FAILURE);
}
for (i=0; i < NMEMB; i++) {
strcpy(mystruct.myword,mystring);
mystruct.myintone = i;
mystruct.myinttwo = i + 5;
}
printf("myword at 12 is %s\n",mystruct[12].myword);
printf("myintone at 12 is %d\n",mystruct[12].myintone);
printf("myinttwo at 12 is %d\n",mystruct[12].myinttwo);
free(mystruct);
return 0;
}
 
M

Martin Ambuhl

Jobs'R'Us said:
Hello again,

I have reproduced my problem with less code. The following compiles
but throws a "Segmentation fault" when run. Am I missing a linker
library?

No, you're screwing up the format specifiers:
Online references would be most welcome.
Your C textbook should tell you what the format specifiers are

[...]
struct
{ char myword[50];
int myintone;
int myinttwo;
} mystruct[5000]; [...]
printf("myintone at 1222 is %s",mystruct[1222].myintone);
printf("myinttwo at 1222 is %s",mystruct[1222].myinttwo);

The specifier %s expects a something that decays to a pointer to a
string, a zero-terminated array of chars. myintone and myinttwo are not
arrays of chars, nor are they pointers to chars; they are ints.
 
J

Jobs'R'Us

Hello,

You were all correct! The segmentation error was caused by %s, a dumb
typo. The following comiples ans runs:

//mine
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
struct
{
char myword[50];
int myintone;
int myinttwo;
} mystruct[5000];

char mystring[] = "mystring";

int i;
for(i=0;i < 5000;i++)
{
strcpy(mystruct.myword,mystring);
mystruct.myintone = i;
mystruct.myinttwo = i + 5;
}

printf("myword at 1222 is %s",mystruct[1222].myword);
printf("myintone at 1222 is %d",mystruct[1222].myintone);
printf("myinttwo at 1222 is %d",mystruct[1222].myinttwo);

return 0;
}
//end mine

And Pete works as well:
//begin pete's code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NMEMB 50000

int main(void)
{
struct {
char myword[50];
int myintone;
int myinttwo;
} *mystruct;
char mystring[] = "mystring";
int i;

mystruct = malloc(NMEMB * sizeof *mystruct);
if (mystruct == NULL) {
fputs("I'm tired\n", stderr);
exit(EXIT_FAILURE);
}
for (i=0; i < NMEMB; i++) {
strcpy(mystruct.myword,mystring);
mystruct.myintone = i;
mystruct.myinttwo = i + 5;
}
printf("myword at 12 is %s\n",mystruct[12].myword);
printf("myintone at 12 is %d\n",mystruct[12].myintone);
printf("myinttwo at 12 is %d\n",mystruct[12].myinttwo);
free(mystruct);
return 0;
}
//end Pete's code

Best regards to all, I can go finish programming my missile warhead
now!
;)
Kirst
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top