By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

struct with pointers

P: n/a
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[i]= size;
size--;
}
/*fill floats*/

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

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

}

return EXIT_SUCCESS;
}
Jun 7 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Carramba wrote:
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
Jun 7 '07 #2

P: n/a
On Jun 7, 3:49 pm, Carramba <carra...@example.comwrote:
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!
<... stuff omitted ...>
>
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],i,tb->f[i]);
4 arguments, 3 format specifiers.

-David

Jun 7 '07 #3

P: n/a
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.

--
Er*********@sun.com
Jun 7 '07 #4

P: n/a
"Carramba" <ca******@example.comwrote in message
news:46***********************@news.luth.se...
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

Jun 7 '07 #5

P: n/a
On Jun 8, 7:49 am, Carramba <carra...@example.comwrote:
/*fill ints*/
for(i=0;i<9;i++) {
tb->t[i]= size;
size--;
}
/*fill floats*/
for(i=0;i<9;i++) {
tb->f[i]= 0.01*(i+1);
}

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

Jun 7 '07 #6

P: n/a
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:
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[i]= size;
size--;
}
/*fill floats*/

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

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

}

return EXIT_SUCCESS;
}
Jun 8 '07 #7

P: n/a
On 6 8 , 2 50 , Carramba <carra...@example.comwrote:
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:
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));
You have not allocate memory space for tb.
>
/*fill ints*/
for(i=0;i<9;i++) {
tb->t[i]= size;
size--;
}
/*fill floats*/
for(i=0;i<9;i++) {
tb->f[i]= 0.01*(i+1);
}
for(i=0;i<10;i++) {
printf("tb->t[%d]=%d ;tb->f[%d]\n",i,tb->t[i],i,tb->f[i]);
}
return EXIT_SUCCESS;
}

Jun 8 '07 #8

P: n/a

"Carramba" <ca******@example.comwrote in message
news:f4**********@aioe.org...
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.
(snip)
--
Fred L. Kleinschmidt
Jun 8 '07 #9

P: n/a
On Fri, 08 Jun 2007 08:50:59 +0200, Carramba <ca******@example.com>
wrote:
>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[i]= size;
size--;
}
/*fill floats*/

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

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

}

return EXIT_SUCCESS;
}

Remove del for email
Jun 9 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.