Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing structure pointers as arguments

15 views
Skip to first unread message

Aamer Nazir

unread,
Aug 14, 1998, 3:00:00 AM8/14/98
to
I was working on passing structures as arguments and pointers to structures
as arguments. I was able to come up with the code below. Here I have a
structure of type test and I am passing pointer to this structure in
exec_structure(). But before that I have to declare the structure in
main(or any calling function). Is there any way that I could actually
instead of declaring the structure (e.g here it is struct test structure;)
declare a pointer to the structure and then pass pointer to the pointer to
structure in the called function. If yes could anyone modify this code for
me.
Thanks in advance,
Aamer Nazir.

#include<stdio.h>
#include<stdlib.h>
void exec_structure(struct test *structure);

struct test
{

char name[10];
char country[10];
};

void main(void)
{
struct test structure;
exec_structure(&structure);


}
void exec_structure(struct test *structure)
{
printf("Please enter your name\n");
fgets(structure->name,5,stdin);
printf("\nThe name you just entered is %s\n",structure->name);
}

vijaya

unread,
Aug 14, 1998, 3:00:00 AM8/14/98
to
The main can be modified as follows:
void main(void)
{
//struct test structure;
struct test* structure= new struct test;
exec_structure(structure);
delete structure;

}

The exec_structure() remains the same.

User923005

unread,
Aug 14, 1998, 3:00:00 AM8/14/98
to
vijaya forgot something...

>The main can be modified as follows:
No it can't

>void main(void)
main returns int in C.

>{
>//struct test structure;
This is a syntax error in C. C9X will recognize these comments

>struct test* structure= new struct test;

No new operator in C. That's C++.

>exec_structure(structure);
>delete structure;
No delete operator in C. That's C++.
You forgot to return a status.
>}
If this were news:comp.lang.c++ your answer might have made some sense. Over
here, it is pure gibberish.


--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software? Algorithms? Pubs? http://www.infoseek.com

Cyrand

unread,
Aug 14, 1998, 3:00:00 AM8/14/98
to
vijaya wrote:
>
> The main can be modified as follows:
> void main(void)

/* Wrong, main() always returns int. */

> {
> //struct test structure;

/* // style comments aren't in the C standard yet. They will be,
eventually. */

> struct test* structure= new struct test;

^^^^^^^^^^^^^^^
/* Try test *structure */

/* New is a C++ keyword. It is not used in C. Use malloc() instead.
Then check malloc() for success.*/

> exec_structure(structure);

/* This is the only correct line in the program besides { and } */

> delete structure;

/* Delete is C++, not C! */
> }

Here is a corrected version of the program above:

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

void exec_structure(struct test *structure);

struct test
{

char name[10];
char country[10];
};

int main(void) /* MAIN ALWAYS RETURNS INT */
{
/* Declare and initialize *structure in one go. */

struct test *structure = malloc(sizeof(struct test));

/* If malloc() returned NULL, it's time to bail */

if(structure == NULL)
{
exit(EXIT_FAILURE);
}

/* If you are passing a pointer don't use &. If you are passing a
structure, do use & */

exec_structure(structure);

/* This allows the memory that was being pointed to to be
reused */

free(structure);
}

void exec_structure(struct test *structure)
{
printf("Please enter your name\n");
fgets(structure->name,5,stdin);
printf("\nThe name you just entered is %s\n",structure->name);
}


--
Email: cyr...@hotmail.com
(supporter of the campaign against grumpiness in c.l.c)
"The most cost effective way to make your business
explode!" --An unknown spammer

Cyrand

unread,
Aug 14, 1998, 3:00:00 AM8/14/98
to
User923005 wrote:
>
> vijaya forgot something...

> >void main(void)
> main returns int in C.

<snip C++ code>

> If this were news:comp.lang.c++ your answer might have made some sense. Over
> here, it is pure gibberish.

I thought that C++ still required main to return int, and required that
you check for success after allocating memory. So my guess is it would
be gibberish there also. (If not, C++ is clearly a broken language)

User923005

unread,
Aug 14, 1998, 3:00:00 AM8/14/98
to
Cyrand:

>I thought that C++ still required main to return int, and required that
>you check for success after allocating memory. So my guess is it would
>be gibberish there also. (If not, C++ is clearly a broken language)
I said "some sense" not "would be sensible."
But you make good points. Cyrand is an interesting name, too.
It reminds me of Kurosh Edalati, a college study partner of mine. Only because
'Edalati' means 'Heir to Cyrus the Great' and Cyrand sounds like Cyrus.

Go figure.

Cyrand

unread,
Aug 14, 1998, 3:00:00 AM8/14/98
to
User923005 wrote:
>But you make good points. Cyrand is an interesting name, too.
>It reminds me of Kurosh Edalati, a college study partner of mine. Only because
>'Edalati' means 'Heir to Cyrus the Great' and Cyrand sounds like Cyrus.
>
>Go figure.

Well, I guess that's as good an association as any. The name actually
originated as the name of one of my characters in AD&D. I'm not even
sure if it's an actual name in any language!

Stephan Wilms

unread,
Aug 14, 1998, 3:00:00 AM8/14/98
to
Aamer Nazir wrote:
>
> Is there any way that I could actually
> instead of declaring the structure (e.g here it is struct test structure;)
> declare a pointer to the structure and then pass pointer to the pointer to
> structure in the called function. If yes could anyone modify this code for
> me.

Hi Aamer Nazir,

What you request is essentiall no problem at all, but it is not
very useful. You can simply write in main:
struct test *structure;
end call "exec_structure()" like this:
exec_structure( structure );

*BUT* (big and important but): now you have a structure with *no*
memory assigned to it. You have a pointer pointing to nowhere.
Dereferencing this pointer in any way will cause undefined behaviour,
ie. very likely crash your program. If you declare a normal structure
the compiler supplies the memory for storing the structure. If you
declare just a pointer, you'll have to provide the memory for the
actual structure yourself.

> void main(void)

You *should know* that this is wrong ! You've been around for some
time and have received a lot of advice here in c.l.c. By now you
should have learned that "int" is the only correct return type
for "main()".

> fgets(structure->name,5,stdin);

This line will crash, if "structure" is just a pointer.

Stephan
(initiator of the campaign against grumpiness in c.l.c)

John Kugelman

unread,
Aug 14, 1998, 3:00:00 AM8/14/98
to
vijaya wrote:
>
> The main can be modified as follows:
> void main(void)
> {
> //struct test structure;

> struct test* structure= new struct test;
> exec_structure(structure);
> delete structure;
>
> }

4 out of 5 lines completely wrong. Yes, it *can* be modified like that,
but then it wouldn't work.

--
John Kugelman. kuge...@mnsinc.com

I believe we can change anything.
I believe in my dream.
- Joe Satriani

Aamer Nazir

unread,
Aug 15, 1998, 3:00:00 AM8/15/98
to
Everything worked well except the fact the my compiler didn't automatically
cast the pointer to type (struct test*) but after doing that everything
worked fine.Thanks.
Aamer.


Cyrand <cyr...@juno.com> wrote in article <35D3D4B9...@juno.com>...


> vijaya wrote:
> >
> > The main can be modified as follows:
> > void main(void)
>

> /* Wrong, main() always returns int. */
>
> > {
> > //struct test structure;
>
> /* // style comments aren't in the C standard yet. They will be,
> eventually. */
>

> > struct test* structure= new struct test;

Aamer Nazir

unread,
Aug 15, 1998, 3:00:00 AM8/15/98
to
Yes by changing the code I meant modifying it so that it was correct. As I
didn't have a pointer in my program,there was no way I could have allocated
sufficient memory for the pointer. I know I was wrong in the void
main(void) definition, I would avoid that in the future. It's just the old
habit taking some time to die :)

Regards,
Aamer.

Stephan Wilms <Stepha...@CWA.de> wrote in article
<35D3D8...@CWA.de>...

> (initiator of the campaign against grumpiness in c.l.c)
>

Aaron Crane

unread,
Aug 15, 1998, 3:00:00 AM8/15/98
to
In article <01bdc7de$b317f200$2e41e4cf@default>,

"Aamer Nazir" <naz...@cadvision.com> writes:
> Everything worked well except the fact the my compiler didn't automatically
> cast the pointer to type (struct test*) but after doing that everything
> worked fine.Thanks.

Let me get this straight. You gave this to your compiler:

> > struct test
> > {
> >
> > char name[10];
> > char country[10];
> > };
> >
> > int main(void) /* MAIN ALWAYS RETURNS INT */
> > {
> > /* Declare and initialize *structure in one go. */
> >
> > struct test *structure = malloc(sizeof(struct test));

and it complained about an `uncast assignment between (void *) and pointer
to object type' or some such? In that case, I suggest that in future you
compile your C programs with a C compiler, and reserve your C++ compiler for
C++ programs. If your compiler claims to translate C and gave you an error
for that code, then you should throw it away.

--
Aaron Crane <aaron...@pobox.com> <URL:http://pobox.com/~aaronc/>

Lawrence Kirby

unread,
Aug 15, 1998, 3:00:00 AM8/15/98
to
In article <01bdc7de$b317f200$2e41e4cf@default>
naz...@cadvision.com "Aamer Nazir" writes:

>Everything worked well except the fact the my compiler didn't automatically
>cast the pointer to type (struct test*) but after doing that everything
>worked fine.Thanks.

>Aamer.

An explicit cast is not required in C. If your compiler is rejecting the
code because of this then you did something wrong and that something is
*not* fixed by adding a cast. The most likely causes are that you didn't
include <stdlib.h> or you are trying to compile C code with a C++ compiler
(which is of course a serious error). If you wanted the code to be C++ in
the first place then you are in the wrong newsgroup.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


Aamer Nazir

unread,
Aug 16, 1998, 3:00:00 AM8/16/98
to

> If your compiler claims to translate C and gave you an error
>for that code, then you should throw it away.

????? I am sorry I cant throw it away. Thanks for the info but you could
have saiid the same thing in a better manner.


Richard Stamp

unread,
Aug 16, 1998, 3:00:00 AM8/16/98
to
Aamer Nazir wrote in message <01bdc90b$a1293ee0$3241e4cf@default>...

Let me try:

If your C compiler does not let you convert silently between malloc's return
type and another object pointer type, there is something seriously wrong.
One explanation is that you are invoking it as a C++ compiler, another is
that you are not invoking it in ANSI mode (some vendors' compilers need this
to be turned on specially). If you do not have the ability to turn on ANSI
compliance, then frankly needing to cast here will be the least of your
worries.

Another possibility is that you forgot to include <stdlib.h> so the compiler
thinks that malloc() returns an int.

Any of these indicates a potentially serious error, so you really do need to
track down the problem and eliminate it.
--
Richard Stamp, Cambridge, UK


Aamer Nazir

unread,
Aug 18, 1998, 3:00:00 AM8/18/98
to
Yes I got it now. Thanks.
Aamer.

Richard Stamp <richar...@acm.org> wrote in article
<6r7m80$h43$1...@pegasus.csx.cam.ac.uk>...

Ben Bullock

unread,
Aug 23, 1998, 3:00:00 AM8/23/98
to
Aamer Nazir wrote:

> I was working on passing structures as arguments and pointers to structures
> as arguments. I was able to come up with the code below. Here I have a
> structure of type test and I am passing pointer to this structure in
> exec_structure(). But before that I have to declare the structure in

> main(or any calling function). Is there any way that I could actually


> instead of declaring the structure (e.g here it is struct test structure;)
> declare a pointer to the structure and then pass pointer to the pointer to
> structure in the called function.

It's certainly possible to declare a pointer to a pointer
to a structure. In your example you can write

void exec_structure(struct test ** structure);

Then in the function you can access elements of it by

(*structure)->name

etc.

To call it you might write

structure test * x;
exec_structure (& x);

You need to allocate memory for x somehow as well.

For example you could write

structure test * x, y;
x = & y;
exec_structure (& x)

in `main', or you could write

* x = malloc (sizeof (struct structure));

in `exec_structure'.

--
Ben Bullock

http://www.hayamasa.demon.co.uk/

Peter Shaggy Haywood

unread,
Aug 25, 1998, 3:00:00 AM8/25/98
to
Groovy hepcat Aamer Nazir was jivin' on 15 Aug 98 05:53:48 GMT in
comp.lang.c.
Re: Passing structure pointers as arguments's a cool scene! Dig it!

>Everything worked well except the fact the my compiler didn't automatically
>cast the pointer to type (struct test*) but after doing that everything
>worked fine.Thanks.
>Aamer.

Then you might have forgotten to include stdlib.h. That is where
malloc() is declared, so it should be included every time you want to
use malloc(). The only other possibility I can think of is that you
are using a C++ compiler to compile your C program. In C++, conversion
from one pointer type to another must be done explicitly with a cast;
whereas conversion is implicit in straight C.
In fact, explicitly casting the pointer returned from malloc(), in
C, can cause problems when you forget to include stdlib.h, because
there isn't a declaration in scope, and the compiler assumes the
function returns int. As mentioned, C++ requires the explicit cast, so
there's not much you can do about that.
--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============


Lawrence Kirby

unread,
Aug 25, 1998, 3:00:00 AM8/25/98
to
In article <35dd45d8...@news.aardvark.apana.org.au>
phay...@aardvark.apana.org.au.REMOVE.THIS.BIT
"Peter "Shaggy" Haywood" writes:

>Groovy hepcat Aamer Nazir was jivin' on 15 Aug 98 05:53:48 GMT in
>comp.lang.c.
>Re: Passing structure pointers as arguments's a cool scene! Dig it!
>
>>Everything worked well except the fact the my compiler didn't automatically
>>cast the pointer to type (struct test*) but after doing that everything
>>worked fine.Thanks.
>>Aamer.
>
> Then you might have forgotten to include stdlib.h.

This was discussed 1-2 weeks ago. The code posted did #include <stdlib.h>
and the compiler was complaining about converting void * to struct test *.

>That is where
>malloc() is declared, so it should be included every time you want to
>use malloc(). The only other possibility I can think of is that you
>are using a C++ compiler to compile your C program. In C++, conversion
>from one pointer type to another must be done explicitly with a cast;
>whereas conversion is implicit in straight C.

That was the explanation suggested at the time.

tst...@my-dejanews.com

unread,
Aug 26, 1998, 3:00:00 AM8/26/98
to
In article <904088...@genesis.demon.co.uk>,
fr...@genesis.demon.co.uk wrote:
[snip]

>
> This was discussed 1-2 weeks ago. The code posted did #include <stdlib.h>
> and the compiler was complaining about converting void * to struct test *.
>
> >That is where
[snip]

From this and his other posts, I get the feeling that he has mistaken the
advice to read for at least one month with read [and post] once a month.

;-)

--
Tristan Styles

Failure is not an option
It is Standard Operating Procedure

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

0 new messages