structure member access issue

W

Waxhead

Note: I'm not experienced with newsgroups so my apologies if I've done
any errors.

In the function dosomething() I would like to skip the workingstuffptr
"workaround". is there any way I can use stuffptr directly without
using a temp?

Thanks in advance.



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

typedef struct _stuff
{
unsigned int value;
} STUFF, *LPSTUFF;

void dosomething(LPSTUFF *stuffptr)
{
STUFF *workingstuffptr;

workingstuffptr = *stuffptr;
workingstuffptr->value = 5;

stuffptr->id = 5; /* Can't be done */

}

int main(void)
{
LPSTUFF somestuff;

somestuff = malloc(sizeof(STUFF));
if(!somestuff)
return 5;
somestuff->value = 10;

dosomething(&somestuff);

printf("Value = %d\n",somestuff->value);
return 0;
}
 
I

Irrwahn Grausewitz

Waxhead said:
Note: I'm not experienced with newsgroups so my apologies if I've done
any errors.

In the function dosomething() I would like to skip the workingstuffptr
"workaround". is there any way I can use stuffptr directly without
using a temp?

Thanks in advance.



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

typedef struct _stuff

Identifiers beginning with an underscore are reserved for the
implementation.
{
unsigned int value;
} STUFF, *LPSTUFF;

Note that hiding structural types with typedef is often considered
very bad style, hiding pointer types with a typedef as well.
void dosomething(LPSTUFF *stuffptr)

Why do you pass a pointer-to-pointer? Actually this looks like a
good example how a programmer got confused by his own typedef'ing
practice.
{
STUFF *workingstuffptr;

Why not be use LPSTUFF here as well? At least be consistent with your
typename aliases.
workingstuffptr = *stuffptr;
workingstuffptr->value = 5;

stuffptr->id = 5; /* Can't be done */

An for two obvious reasons:
1. stuffptr is a pointer-to-a-pointer-to-struct _stuff
2. your struct _stuff has no member named id declared.
}

int main(void)
{
LPSTUFF somestuff;

somestuff = malloc(sizeof(STUFF));

The c.l.c approved idiom for this is:

somestuff = malloc(sizeof *somestuff);

At least you didn't commit the crime to cast the result of malloc. :)
if(!somestuff)
return 5;

5 is not a portable return value for main. Since you already included
stdlib.h EXIT_FAILURE and EXIT_SUCCESS are at your command.
somestuff->value = 10;

dosomething(&somestuff);

Blindly adding levels of indirection, probably because the compiler
barfed, is not the solution
printf("Value = %d\n",somestuff->value);

%d is not the right format specifier to print an unsigned int. Use %u
instead.
return 0;
}

For a beginner your code is not too bad, actually (I've definitely
seen worse), but certainly no cigar. :)

Conclusion: throw out those hideous typedefs, and then rewrite the
program logic. Below you find a corrected and cleaned up version of
your code, don't look if you want to learn by doing it yourself.

..
..
..
..
..
..
..
..
..
..
..
..

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

struct stuff
{
unsigned int value;
};

void dosomething(struct stuff *stuffptr)
{
stuffptr->value = 5;
}

int main(void)
{
struct stuff *somestuff;

somestuff = malloc(sizeof *somestuff);
if(somestuff == NULL)
return EXIT_FAILURE;

somestuff->value = 10;
dosomething(somestuff);
printf("Value = %u\n",somestuff->value);
return EXIT_SUCCESS;
}


Best regards
 
E

Eric Sosman

Waxhead said:
Note: I'm not experienced with newsgroups so my apologies if I've done
any errors.

In the function dosomething() I would like to skip the workingstuffptr
"workaround". is there any way I can use stuffptr directly without
using a temp?

Thanks in advance.



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

typedef struct _stuff
{
unsigned int value;
} STUFF, *LPSTUFF;

void dosomething(LPSTUFF *stuffptr)
{
STUFF *workingstuffptr;

workingstuffptr = *stuffptr;
workingstuffptr->value = 5;

stuffptr->id = 5; /* Can't be done */

(*stuffptr)->id = 5; /* Can be */
 
E

Eric Sosman

Irrwahn said:
Nice try, but: no. :)

Why not, pray? Oh, I see: the O.P. switched gears from
"value" to "id" somewhere in mid-stream, but assuming that
was just a typo, the (*stuffptr)->element syntax is correct.
 
I

Irrwahn Grausewitz

Eric Sosman said:
Why not, pray? Oh, I see: the O.P. switched gears from
"value" to "id" somewhere in mid-stream, but assuming that
was just a typo, the (*stuffptr)->element syntax is correct.

Correct, and to be fair, I should've said so. :]
 
W

Waxhead

Well thanks for pointing out my errors (as I expected). and yes I
forgot to change ->id to ->value since this was just a quick copy of
some code I have running ;)

Thanks for the help ;)
 
E

Emmanuel Delahaye

Waxhead wrote on 10/09/05 :
typedef struct _stuff
{
unsigned int value;
} STUFF, *LPSTUFF;

void dosomething(LPSTUFF *stuffptr)
{
STUFF *workingstuffptr;

workingstuffptr = *stuffptr;
stuffptr->id = 5; /* Can't be done */

'id' is not part of the structure. Let's say you want 'value'.

'stuffptr' being a pointer to a pointer, you need an extra indirection:

(*stuffptr)->id = 5;


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
 
E

Emmanuel Delahaye

Irrwahn Grausewitz wrote on 10/09/05 :
Identifiers beginning with an underscore are reserved for the
implementation.

Well, strictly speaking, and AFAIK, id's beginning by a _ and followed
by a lowercase are not reserved. But they are ugly enough to me to
avoid them.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
 
I

Irrwahn Grausewitz

Emmanuel Delahaye said:
Irrwahn Grausewitz wrote on 10/09/05 :

Well, strictly speaking, and AFAIK, id's beginning by a _ and followed
by a lowercase are not reserved.

Oops, you're right.
But they are ugly enough to me to
avoid them.

Same to me. :)

Best Regards
 
I

Irrwahn Grausewitz

Waxhead said:
Well thanks for pointing out my errors (as I expected). and yes I
forgot to change ->id to ->value since this was just a quick copy of
some code I have running ;)

Thanks for the help ;)

No problem, but please preserve some context when posting replies,
it's otherwise difficult to tell to whom or what you are responding.

-- If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers. --

Best Regards
 
K

Keith Thompson

Waxhead said:
Well thanks for pointing out my errors (as I expected). and yes I
forgot to change ->id to ->value since this was just a quick copy of
some code I have running ;)

This is a good illustration of why, when posting code, you should
*always* cut-and-paste the actual code you fed to the compiler. If
you try to transcribe it manually, you'll inevitably make mistakes (I
do this myself), and it may be impossible for us to guess what your
actual problem is.
 
J

Joe Wright

Keith said:
This is a good illustration of why, when posting code, you should
*always* cut-and-paste the actual code you fed to the compiler. If
you try to transcribe it manually, you'll inevitably make mistakes (I
do this myself), and it may be impossible for us to guess what your
actual problem is.

Nit-pick, but it's copy-and-paste you want. Cutting the code out of the
source file would not be "A Good Thing".
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top