473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Does this works

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

Jan 30 '07 #1
12 1440
raghu wrote:
Hello Everyone,

I have a doubt for a long time. consider a structure
struct A{
int z;
}
Missing ;
struct n{
struct A *a;
int *y;
}*st;
will this works
What happened when you tried it?
st -a -z = 10;
What do you think st points to? Where is it initialised?

--
Ian Collins.
Jan 30 '07 #2
On Jan 30, 10:45 am, Ian Collins <ian-n...@hotmail.comwrote:
raghu wrote:
Hello Everyone,
I have a doubt for a long time. consider a structure
struct A{
int z;
}

Missing ;
struct n{
struct A *a;
int *y;
}*st;
will this works

What happened when you tried it?
st -a -z = 10;

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

--
Ian Collins.
oops sorry struct A ends with ' ; ' and this statement
st -a -z = 10;
initialized in init function which populates all the values of the
structure.

Jan 30 '07 #3
raghu <ra*********@gmail.comwrote:
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)
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Jan 30 '07 #4
raghu wrote:
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

Jan 30 '07 #5
"raghu" <ra*********@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
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.

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 30 '07 #6
In article <Qa******************************@giganews.com>,
David T. Ashley <dt*@e3ft.comwrote:
....
>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!)

Jan 30 '07 #7
David T. Ashley wrote, On 30/01/07 06:58:
"raghu" <ra*********@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
>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.
You just did!
Yes, it will work just fine.
Which is what the others said.

<snip>
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.
--
Flash Gordon
Jan 30 '07 #8
"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
news:a8************@news.flash-gordon.me.uk...
David T. Ashley wrote, On 30/01/07 06:58:
>"raghu" <ra*********@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegr oups.com...
>>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.

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

Which is what the others said.

<snip>
>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?
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.

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 30 '07 #9
David T. Ashley wrote, On 30/01/07 16:20:
"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
news:a8************@news.flash-gordon.me.uk...
>David T. Ashley wrote, On 30/01/07 06:58:
<snip>
>>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?

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. :-)
>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.
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.
--
Flash Gordon
Jan 30 '07 #10
On Jan 30, 11:13 am, Bernhard Holzmayer
<Holzmayer.Bernh...@deadspam.comwrote:
raghu wrote:
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- 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

Feb 1 '07 #11
raghu wrote:
>
.... 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[i] = 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 */

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 2 '07 #12
On 29 Jan 2007 21:59:41 -0800, "raghu" <ra*********@gmail.comwrote:
>On Jan 30, 10:45 am, Ian Collins <ian-n...@hotmail.comwrote:
>raghu wrote:
Hello Everyone,
I have a doubt for a long time. consider a structure
struct A{
int z;
}

Missing ;
struct n{
struct A *a;
int *y;
}*st;
will this works

What happened when you tried it?
st -a -z = 10;

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

--
Ian Collins.

oops sorry struct A ends with ' ; ' and this statement
st -a -z = 10;

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
Feb 4 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

162
by: Isaac Grover | last post by:
Hi everyone, Just out of curiosity I recently pointed one of my hand-typed pages at the W3 Validator, and my hand-typed code was just ripped to shreds. Then I pointed some major sites...
7
by: Todd Cary | last post by:
I inherited an application where the Flyout works in Firefox but not in IE. My JavaScript background is sparse, so I am not sure how to approach the problem. Any suggestions are welcomed! The...
1
by: Georg Scholz | last post by:
Hello, The class "Control" contains a documented Property "ControlType". So for example, in a form, you can write code like this: Dim c as control set c = me.Controls("textbox1") if...
5
by: rogsonl | last post by:
My computer was moved last week, and the company changed the network groups we work on. As a result, one of the main benefits from Whidbey (database connectivity) no longer works. Situation: 1....
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
11
by: minnesotti | last post by:
Hi there, I subscribed to a photographic pictures-hosting website which is heavy on JavaScript. My preferred latest browser Mozilla Firefox does not work with it -- no pictures are displayed and...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
3
by: wwwmike | last post by:
For some reasons my HTTP Redirect for 404 errors does not work for certain files like .gif .jpg on IIS Version: 5.1, but it works perfectly with the VisualStudio2005 internal webserver ...
5
by: antonyliu2002 | last post by:
Hi, It looks like so many people are having problems with the javascript submit in firefox. I searched around, but haven't found a solution yet. Mostly, people were saying, try this or try...
1
by: rockdale | last post by:
Hi: I have a web application which runs fine on our production server. But now when I install abother production server, it gets "System.NullReferenceException: Object reference not set to an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.