struct with pointers

C

Carramba

Hi!
I want to have a struct representing a table, with int's and float.
But I seem to get only stuckdump.. and not really sure why , hope you
can help!

Thanxk you in advance

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

typedef struct {
int *t;
float *f;

} Table;

Table *tb;

int main(void) {

int size, i;

/* init table*/
size = 10;
/* allocate array for ints */
tb->t = malloc(size * sizeof*(tb->t));
/* allocate array for foats */
tb->f = malloc(size * sizeof *(tb->f));

/*fill ints*/
for(i=0;i<9;i++) {
tb->t= size;
size--;
}
/*fill floats*/

for(i=0;i<9;i++) {
tb->f= 0.01*(i+1);
}

for(i=0;i<10;i++) {
printf("tb->t[%d]=%d ;tb->f[%d]\n",i,tb->t,i,tb->f);

}

return EXIT_SUCCESS;
}
 
D

Default User

Carramba said:
Hi!
I want to have a struct representing a table, with int's and float.
But I seem to get only stuckdump.. and not really sure why , hope you
can help!

Thanxk you in advance

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

typedef struct {
int *t;
float *f;

} Table;

Table *tb;

int main(void) {

int size, i;

/* init table*/
size = 10;
/* allocate array for ints */
tb->t = malloc(size * sizeof*(tb->t));


You didn't allocate memory for tb.




Brian
 
D

David Resnick

Hi!
I want to have a struct representing a table, with int's and float.
But I seem to get only stuckdump.. and not really sure why , hope you
can help!
Table *tb;

int main(void) {

int size, i;

/* init table*/
size = 10;
/* allocate array for ints */
tb->t = malloc(size * sizeof*(tb->t));

tb has not been allocated at this point. The dereference here is
causing your problems. You could try doing
Table tb;
and replacing "->" with ".". Or allocating tb like
tb = malloc(sizeof *tb);

BTW, checking that actually malloc worked is always a good idea...
Also compiling with warnings on, while it wouldn't have identified
this problem, might have noticed that you have an argument mismatch
here:
printf("tb->t[%d]=%d ;tb->f[%d]\n",i,tb->t,i,tb->f);

4 arguments, 3 format specifiers.

-David
 
E

Eric Sosman

Carramba wrote On 06/07/07 15:49,:
Hi!
I want to have a struct representing a table, with int's and float.
But I seem to get only stuckdump..

It was probably a typo, but it makes a nice
addition to the lexicon.
 
K

Karl Malbrain

Carramba said:
Hi!
I want to have a struct representing a table, with int's and float.
But I seem to get only stuckdump.. and not really sure why , hope you can
help!

Thanxk you in advance

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

typedef struct {
int *t;
float *f;

} Table;

Table *tb;

You want:
Table tb[1];

karl m
 
O

Old Wolf

/*fill ints*/
for(i=0;i<9;i++) {
tb->t= size;
size--;
}
/*fill floats*/
for(i=0;i<9;i++) {
tb->f= 0.01*(i+1);
}

for(i=0;i<10;i++) {
printf("tb->t[%d]=%d ;tb->f[%d]\n",i,tb->t,i,tb->f);


As well as what other people pointed out; you're only
filling in nine values in each array but then you
print 10, so you would get undefined behaviour
when trying to print the tenth.
 
C

Carramba

Thank you!
Another questions regarding this configuration.
Do I have to free() every pointer in struc or it it enough to free(tb),
struct) and all pointers in it will also by freed?

Carramba skrev:
 
J

Jinsere

Thank you!
Another questions regarding this configuration.
Do I have to free() every pointer in struc or it it enough to free(tb),
struct) and all pointers in it will also by freed?

Carramba skrev:

You have not allocate memory space for tb.
/*fill ints*/
for(i=0;i<9;i++) {
tb->t= size;
size--;
}
/*fill floats*/

for(i=0;i<9;i++) {
tb->f= 0.01*(i+1);
}

for(i=0;i<10;i++) {
printf("tb->t[%d]=%d ;tb->f[%d]\n",i,tb->t,i,tb->f);

return EXIT_SUCCESS;
}
 
F

Fred Kleinschmidt

Carramba said:
Thank you!
Another questions regarding this configuration.
Do I have to free() every pointer in struc or it it enough to free(tb),
struct) and all pointers in it will also by freed?

1) Don't toppost.

2) Yes, you must free every malloc'd pointer. Freeing the struct does
not free space created for pointers in the struct.
For each call to malloc you must later call free.
 
B

Barry Schwarz

Thank you!
Another questions regarding this configuration.
Do I have to free() every pointer in struc or it it enough to free(tb),
struct) and all pointers in it will also by freed?

You must free any pointers in the struct that point to allocated space
and you must do so before you free the pointer to the struct itself.
Carramba skrev:
Hi!
I want to have a struct representing a table, with int's and float.
But I seem to get only stuckdump.. and not really sure why , hope you
can help!

Thanxk you in advance

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

typedef struct {
int *t;
float *f;

} Table;

Table *tb;

int main(void) {

int size, i;

/* init table*/
size = 10;
/* allocate array for ints */
tb->t = malloc(size * sizeof*(tb->t));
/* allocate array for foats */
tb->f = malloc(size * sizeof *(tb->f));

/*fill ints*/
for(i=0;i<9;i++) {
tb->t= size;
size--;
}
/*fill floats*/

for(i=0;i<9;i++) {
tb->f= 0.01*(i+1);
}

for(i=0;i<10;i++) {
printf("tb->t[%d]=%d ;tb->f[%d]\n",i,tb->t,i,tb->f);

}

return EXIT_SUCCESS;
}



Remove del for email
 

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
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top