Does this works

R

raghu

Hello Everyone,

I have a doubt for a long time. consider a structure
struct A{
int z;
}
struct n{
struct A *a;
int *y;
}*st;
will this works
st -> a -> z = 10;


Thanks in adv
 
R

raghu

Missing ;


What happened when you tried it?


What do you think st points to? Where is it initialised?

oops sorry struct A ends with ' ; ' and this statement

initialized in init function which populates all the values of the
structure.
 
N

Nelu

raghu said:
Hello Everyone,

I have a doubt for a long time. consider a structure
struct A{
int z;
}

You forgot the ; after }
struct n{
struct A *a;
int *y;
}*st;

will this works
st -> a -> z = 10;

Yes, with two conditions:
- you succesfully allocate memory for st
- you succesfully allocate memory for st->a
(also for y if you decide to use y)
 
B

Bernhard Holzmayer

raghu said:
Hello Everyone,

I have a doubt for a long time. consider a structure
struct A{
int z;
}
struct n{
struct A *a;
int *y;
}*st;
will this works
st -> a -> z = 10;


Thanks in adv

Hi raghu,
except of the missing semicolon, it will certainly work as you describe.
There's no problem as long as you don't access it with illegitim type casts.
You will not know how the content of the structures is physically organized
in memory. But as long as you fetch the data with the same addressing
method during write and read accesses, it is sound coding, and will make
sense in a lot of situations.

There's another structure which might interest you, and which I abundantly
use:
struct sbitwise {
unsigned bit_X:1;
unsigned bit_Y:1;
unsigned bit_Z:1;
unsigned bitfield: 5;
};
union as {
struct sbitwise bitwise;
unsigned complete;
};

union as challenge;


and then I can address it once bitwise, once in a single manner:

challenge.bitwise.bit_X = 1;
challenge.complete=0;

In this case, I address the same address location with different methods,
and this is NOT conformal to the specs, neither ANSI nor ISO.
However, it is very helpful - but you'll have to find out how and if it
works on every machine, on which you'll use it.

Bernhard
 
D

David T. Ashley

raghu said:
Hello Everyone,

I have a doubt for a long time. consider a structure
struct A{
int z;
}
struct n{
struct A *a;
int *y;
}*st;
will this works
st -> a -> z = 10;

Unlike some of the other posters and all compilers, I understand your
question and I won't mention the missing semicolon or the lack of memory
allocation.

Yes, it will work just fine.

From the compiler's point of view, this:

st -> a -> z = 10;

is really this:

((st -> a) -> z) = 10;

and everybody will live happily ever after.

I do understand the reason for your question. Something in the back of your
mind is telling you that you need to use an intermediate pointer. Nope, you
don't need to. "->" is just another operator.

On behalf of the saner members of comp.lang.c, I have to apologize for the
others who were so obsessed with details that weren't really relevant to
your question. They are still living in their parents' basements and
attending Star Trek conventions.
 
K

Kenny McCormack

David T. Ashley said:
On behalf of the saner members of comp.lang.c,

Basically, you and me. The rest of them are nuts.
I have to apologize for the others who were so obsessed with details
that weren't really relevant to your question.

But that's the norm here. Basically, most of the "regs" *aspire* to be
human compilers. That's their fondest desire. To express no more
understanding of people's posts than the typical (unforgiving) C
compiler.
They are still living in their parents' basements and attending Star
Trek conventions.

Good point. (Snicker!)
 
F

Flash Gordon

David T. Ashley wrote, On 30/01/07 06:58:
Unlike some of the other posters and all compilers, I understand your
question and I won't mention the missing semicolon or the lack of memory
allocation.

You just did!
Yes, it will work just fine.

Which is what the others said.

On behalf of the saner members of comp.lang.c, I have to apologize for the
others who were so obsessed with details that weren't really relevant to
your question. They are still living in their parents' basements and
attending Star Trek conventions.

Any particular reason for being insulting or is it that you are
obnoxious by nature?

Pointing out the need for memory allocation is sensible given the number
of times newbies have posted code which showed that they did not know it
is needed. Pointing out the need for the semicolon is also sensible
since in most situations in C you do not need (or want) a semicolon
after a closing brace so the OP might have been surprised by a
compilation error.
 
D

David T. Ashley

Flash Gordon said:
David T. Ashley wrote, On 30/01/07 06:58:

You just did!


Which is what the others said.



Any particular reason for being insulting or is it that you are obnoxious
by nature?

Let me make up for it. I'd be glad to compensate you with a radon test kit
and a Star Trek poster. I understand that radon accumulates in basements,
and that it is the #1 cause of lung cancer after cigarette smoking.
Pointing out the need for memory allocation is sensible given the number
of times newbies have posted code which showed that they did not know it
is needed. Pointing out the need for the semicolon is also sensible since
in most situations in C you do not need (or want) a semicolon after a
closing brace so the OP might have been surprised by a compilation error.

Regrettably, you are correct in this case. I agree with you.

BTW, I was just clowning around.
 
F

Flash Gordon

David T. Ashley wrote, On 30/01/07 16:20:
Let me make up for it. I'd be glad to compensate you with a radon test kit
and a Star Trek poster. I understand that radon accumulates in basements,
and that it is the #1 cause of lung cancer after cigarette smoking.

I'll take the Star Trek poster, but don't bother with the radon test kit
since I don't have a basement. :)
Regrettably, you are correct in this case. I agree with you.
OK.

BTW, I was just clowning around.

<shrug/> It didn't read that way to me, but it's no big deal. All
forgotten here.
 
R

raghu

Hi raghu,
except of the missing semicolon, it will certainly work as you describe.
There's no problem as long as you don't access it with illegitim type casts.
You will not know how the content of the structures is physically organized
in memory. But as long as you fetch the data with the same addressing
method during write and read accesses, it is sound coding, and will make
sense in a lot of situations.

There's another structure which might interest you, and which I abundantly
use:
struct sbitwise {
unsigned bit_X:1;
unsigned bit_Y:1;
unsigned bit_Z:1;
unsigned bitfield: 5;};

union as {
struct sbitwise bitwise;
unsigned complete;

};

union as challenge;

and then I can address it once bitwise, once in a single manner:

challenge.bitwise.bit_X = 1;
challenge.complete=0;

In this case, I address the same address location with different methods,
and this is NOT conformal to the specs, neither ANSI nor ISO.
However, it is very helpful - but you'll have to find out how and if it
works on every machine, on which you'll use it.

Bernhard- Hide quoted text -

- Show quoted text -


Hay then is it possiable to create a data type of 128bit or some thing
more. Actually my question is how to find factorial for 50. that is
50!. I tried out by using the char pointer but failed.Do you have any
idea.

Thank in advance

--ram
 
C

CBFalconer

raghu said:
.... snip ...

Hay then is it possiable to create a data type of 128bit or some
thing more. Actually my question is how to find factorial for 50.
that is 50!. I tried out by using the char pointer but failed. Do
you have any idea.

Try the following:

/* compute factorials, extended range
on a 32 bit machine this can reach fact(15) without
unusual output formats. With the prime table shown
overflow occurs at 101.

Public domain, by C.B. Falconer. 2003-06-22
*/

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

/* 2 and 5 are handled separately
Placing 2 at the end attempts to preserve such factors
for use with the 5 factor and exponential notation
*/
static unsigned char primes[] = {3,7,11,13,17,19,23,29,31,37,
41,43,47,53,57,59,61,67,71,
/* add further primes here -->*/
2,5,0};
static unsigned int primect[sizeof primes]; /* = {0} */

static double fltfact = 1.0;

static
unsigned long int fact(unsigned int n, unsigned int *zeroes)
{
unsigned long val;
unsigned int i, j, k;

#define OFLOW ((ULONG_MAX / j) < val)

/* This is a crude mechanism for passing back values */
for (i = 0; i < sizeof primes; i++) primect = 0;

for (i = 1, val = 1UL, *zeroes = 0; i <= n; i++) {
fltfact *= i; /* approximation */
j = i;
/* extract exponent of 10 */
while ((0 == (j % 5)) && (!(val & 1))) {
j /= 5; val /= 2;
(*zeroes)++;
}
/* Now try to avoid any overflows */
k = 0;
while (primes[k] && OFLOW) {
/* remove factors primes[k] */
while (0 == (val % primes[k]) && OFLOW) {
val /= primes[k];
++primect[k];
}
while (0 == (j % primes[k]) && OFLOW) {
j /= primes[k];
++primect[k];
}
k++;
}

/* Did we succeed in the avoidance */
if (OFLOW) {
#if DEBUG
fprintf(stderr, "Overflow at %u, %lue%u * %u\n",
i, val, *zeroes, j);
#endif
val = 0;
break;
}
val *= j;
}
return val;
} /* fact */

int main(int argc, char *argv[])
{
unsigned int x, zeroes;
unsigned long f;

if ((2 == argc) && (1 == sscanf(argv[1], "%u", &x))) {
if (!(f = fact(x, &zeroes))) {
fputs("Overflow\n", stderr);
return EXIT_FAILURE;
}

printf("Factorial(%u) == %lu", x, f);
if (zeroes) printf("e%u", zeroes);
for (x = 0; primes[x]; x++) {
if (primect[x]) {
printf(" * pow(%d,%d)", primes[x], primect[x]);
}
}
putchar('\n');
printf("or approximately %.0f.\n", fltfact);
return 0;
}
fputs("Usage: fact n\n", stderr);
return EXIT_FAILURE;
} /* main */
 
B

Barry Schwarz

oops sorry struct A ends with ' ; ' and this statement


initialized in init function which populates all the values of the
structure.

You are missing the point. You don't have an object of type struct n.
You do have st, an object of type pointer to struct n, but it doesn't
point to a structure yet because you never initialized it.

You also don't have an object of type struct A.

Once you define or allocate an object of type struct n, you can assign
the address of that object to st. Once *st exists, you can define or
allocate an object of type struct A and assign its address to st->a.
Only after a both pointers point to real objects can you assign a
value to st->a->z.


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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top