Discussion:
puntero a un arreglo de estructuras
(demasiado antiguo para responder)
Ricardo
2006-11-25 17:07:17 UTC
Permalink
Hola amigos, por que el siguiente codigo no funciona, esto me funciona
cuando el tipo es un double pero cuando es un estructura el compilador
gcc no me compila este codigo.

typedef struct sse_struct
{
double sse;
int i; // indice
}sse;

int main(void)
{
sse *sse100;

sse100 = (sse *) malloc((5)*sizeof(sse));

for (i=0; i<5; i++)
{
sse100[i]->sse = unif_rand();
sse100[i]->i = i;

}

free(sse100);
return 0;
}
Pascal Bourguignon
2006-11-25 18:33:05 UTC
Permalink
Post by Ricardo
Hola amigos, por que el siguiente codigo no funciona, esto me funciona
cuando el tipo es un double pero cuando es un estructura el compilador
gcc no me compila este codigo.
typedef struct sse_struct
{
double sse;
int i; // indice
}sse;
int main(void)
{
sse *sse100;
sse100 = (sse *) malloc((5)*sizeof(sse));
for (i=0; i<5; i++)
{
sse100[i]->sse = unif_rand();
sse100[i]->i = i;
}
free(sse100);
return 0;
}
Porque tienes errores.

-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Sat Nov 25 19:32:28

make -k t
cc t.c -o t
t.c: In function `main':
t.c:13: error: `i' undeclared (first use in this function)
t.c:13: error: (Each undeclared identifier is reported only once
t.c:13: error: for each function it appears in.)
make: *** [t] Error 1

Compilation exited abnormally with code 2 at Sat Nov 25 19:32:28
--
__Pascal Bourguignon__ http://www.informatimago.com/

PUBLIC NOTICE AS REQUIRED BY LAW: Any use of this product, in any
manner whatsoever, will increase the amount of disorder in the
universe. Although no liability is implied herein, the consumer is
warned that this process will ultimately lead to the heat death of
the universe.
Ricardo
2006-11-25 22:15:25 UTC
Permalink
Me equivoque el programa es este, y el error que da es el siguiente :

hola.c: In function `main':
hola.c:18: invalid type argument of `->'
hola.c:19: invalid type argument of `->'
hola.c:26:2: warning: no newline at end of file


typedef struct sse_struct
{
double sse;
int i; // indice

}sse;

int main(void)
{
sse *sse100;
int i;

sse100 = (sse *) malloc((5)*sizeof(sse));

for (i=0; i<5; i++)
{
sse100[i]->sse = unif_rand();
sse100[i]->i = i;

}

free(sse100);
return 0;

}
Post by Pascal Bourguignon
Post by Ricardo
Hola amigos, por que el siguiente codigo no funciona, esto me funciona
cuando el tipo es un double pero cuando es un estructura el compilador
gcc no me compila este codigo.
typedef struct sse_struct
{
double sse;
int i; // indice
}sse;
int main(void)
{
sse *sse100;
sse100 = (sse *) malloc((5)*sizeof(sse));
for (i=0; i<5; i++)
{
sse100[i]->sse = unif_rand();
sse100[i]->i = i;
}
free(sse100);
return 0;
}
Porque tienes errores.
-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Sat Nov 25 19:32:28
make -k t
cc t.c -o t
t.c:13: error: `i' undeclared (first use in this function)
t.c:13: error: (Each undeclared identifier is reported only once
t.c:13: error: for each function it appears in.)
make: *** [t] Error 1
Compilation exited abnormally with code 2 at Sat Nov 25 19:32:28
--
__Pascal Bourguignon__ http://www.informatimago.com/
PUBLIC NOTICE AS REQUIRED BY LAW: Any use of this product, in any
manner whatsoever, will increase the amount of disorder in the
universe. Although no liability is implied herein, the consumer is
warned that this process will ultimately lead to the heat death of
the universe.
Pascal Bourguignon
2006-11-25 22:55:39 UTC
Permalink
Post by Ricardo
hola.c:18: invalid type argument of `->'
hola.c:19: invalid type argument of `->'
To use '->' you need to have a pointer to a structure.
Post by Ricardo
hola.c:26:2: warning: no newline at end of file
typedef struct sse_struct
{
double sse;
int i; // indice
}sse;
int main(void)
{
sse *sse100;
int i;
sse100 = (sse *) malloc((5)*sizeof(sse));
for (i=0; i<5; i++)
{
sse100[i]->sse = unif_rand();
sse100[i]->i = i;
Since you've declared that sse100 is a pointer to a structure,
sse100[i] is a structure, not a pointer to a structure. Therefore you
cannot use '->'. Try '.':

sse100[i].sse=unif_rand();
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
----------> http://www.netmeister.org/news/learn2quote.html <-----------
---> http://homepage.ntlworld.com/g.mccaughan/g/remarks/uquote.html <---

__Pascal Bourguignon__ http://www.informatimago.com/
Pascal Bourguignon
2006-11-25 23:00:41 UTC
Permalink
Post by Ricardo
hola.c:18: invalid type argument of `->'
hola.c:19: invalid type argument of `->'
Para usar '->' necesitas un puntero a una estructura.
Post by Ricardo
hola.c:26:2: warning: no newline at end of file
typedef struct sse_struct
{
double sse;
int i; // indice
}sse;
int main(void)
{
sse *sse100;
int i;
sse100 = (sse *) malloc((5)*sizeof(sse));
for (i=0; i<5; i++)
{
sse100[i]->sse = unif_rand();
sse100[i]->i = i;
Como has declarado que sse100 es un puntero a una estructura,
sse100[i] es una estructura, no un puntero a una estructura. Luego no
puedes usar '->'. Proba con '.' :

sse100[i].sse=unif_rand();
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
----------> http://www.netmeister.org/news/learn2quote.html <-----------
---> http://homepage.ntlworld.com/g.mccaughan/g/remarks/uquote.html <---

__Pascal Bourguignon__ http://www.informatimago.com/
Loading...