Pointers in methods??

J

JS

Is it only allowed to make a pointer point to something in a method??



#include <stdio.h>

struct data {

int x;

int y;

};

struct data test, *pp;

pp = &test; //WHY IS THIS ILLEGAL??


main(){


pp = &test;


}



Why is the above code illegal??
 
M

Mark Odell

JS said:
Is it only allowed to make a pointer point to something in a method??



#include <stdio.h>

struct data {

int x;

int y;

};

struct data test, *pp;

pp = &test; //WHY IS THIS ILLEGAL??

You cannot put run-time code (assignment) outside of a function block.
Do not confuse assignment (run-time) with initialization (compile-time),
e.g.

struct data *pp = &test;
main(){


pp = &test;

This, I trust, works.
 
W

Walter Roberson

:Is it only allowed to make a pointer point to something in a method??

C doesn't have "methods"; from your code I suspect you mean
"functions".

:struct data { int x; int y; };
:struct data test, *pp;
:pp = &test; //WHY IS THIS ILLEGAL??

Because you have it on a different line, the setting of pp
is not an initializer but rather a statement.

Try

struct data { int x; int y; };
struct data test, *pp = &test;
 
E

Eric Sosman

JS said:
Is it only allowed to make a pointer point to something in a method??



#include <stdio.h>

struct data {

int x;

int y;

};

struct data test, *pp;

pp = &test; //WHY IS THIS ILLEGAL??

Because it is an executable statement, and an executable
statement must reside inside a function (not "method").
This assignment is illegal for the same reason an exit()
call or a `for' loop would be illegal at this point.

However, you can still achieve your goal by using an
initializer in the declaration of `pp':

struct data test, *pp = &test;

or perhaps more clearly

struct data test;
struct data *pp = &test;

Despite the presence of the `=' this is *not* an assignment
statement; it is a declaration with an initializer. Keep in
mind that C tends to re-use the same character for multiple
purposes. Depending on context, `*' can be the multiplication
operator or the pointer dereference operator or part of a `/*'
or `*/' comment marker or part of the `*=' multiply-and-assign
operator. Just so, `=' can be the assignment operator, or part
of the syntax of an initialization, or part of `==' or `!=' or
`*=' or ... Despite the fact that they both use `=' and have
the same set of symbols on both sides of it, your code and mine
are different: one is an executable statement, and the other
is a variable declaration with an initializer.
 
K

Keith Thompson

JS said:
Is it only allowed to make a pointer point to something in a method??

C has no "methods". It has functions.
#include <stdio.h>

struct data {

int x;

int y;

};

struct data test, *pp;

pp = &test; //WHY IS THIS ILLEGAL??


main(){


pp = &test;


}



Why is the above code illegal??

It's illegal because statements are allowed only within function
bodies.

You can initialize pp if you have an initialization as part of its
declaration:

struct data test, *pp=&test;

or (more clearly IMHO):

struct data test;
struct data *pp = &test;

BTW, indentation makes code easier to read, and you don't need nearly
so many blank lines:

#include <stdio.h>

struct data {
int x;
int y;
};

struct data test;
struct data *pp = &test;

int main(void)
{
pp = &test; /* redundant */
return 0;
}
 
K

Keith Thompson

Mark Odell said:
You cannot put run-time code (assignment) outside of a function
block. Do not confuse assignment (run-time) with initialization
(compile-time), e.g.

struct data *pp = &test;


This, I trust, works.

Initialization doesn't necessarily happen at compile time, though it
probably does if it's outside a function definition.
 
J

Joe Wright

JS said:
Is it only allowed to make a pointer point to something in a method??



#include <stdio.h>

struct data {

int x;

int y;

};

struct data test, *pp;

pp = &test; //WHY IS THIS ILLEGAL??


main(){


pp = &test;


}



Why is the above code illegal??
What's a method?
 
J

JS

Walter Roberson said:
:Is it only allowed to make a pointer point to something in a method??

C doesn't have "methods"; from your code I suspect you mean
"functions".

:struct data { int x; int y; };
:struct data test, *pp;
:pp = &test; //WHY IS THIS ILLEGAL??

Because you have it on a different line, the setting of pp
is not an initializer but rather a statement.

Try

struct data { int x; int y; };
struct data test, *pp = &test;


Thats another thing that confuses me why I need to use "," before *pp.
I have made this in my code.

struct data test *pp;

but will only compile if I write:

struct data test, *pp;
 
J

Jens.Toerring

JS said:
Thats another thing that confuses me why I need to use "," before *pp.
I have made this in my code.
struct data test *pp;
but will only compile if I write:
struct data test, *pp;

Of course, because you defined _two_ variables, 'test' and 'pp' - and
then you need a comma in between the two. That's the same as with

int i, j;

Leaving out the comma is a syntax error.

Regards, Jens
 
T

Tor Rustad

#include <stdio.h>

struct data {
int x;
int y;
};

struct data test;
struct data *pp = &test;

int main(void)
{
pp = &test; /* redundant */
return 0;
}

This makes "test" and "*pp" global variables. It's better style
to use local variables, since this hide these identifiers from
other functions:

int main (void)
{
struct data test, *pp; /* <--- local to function main */

pp = &test;

return 0;
}

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 
J

Joona I Palaste

Eric Sosman said:
(snip)

Because it is an executable statement, and an executable
statement must reside inside a function (not "method").

I write Java code for a living, and a colleague of mine (who works in
the same Java code project) keeps saying "function" when he means
"method". This is absurd - C functions and Java methods are
conceptually the same thing, but you have to call them "functions" in
C and "methods" in Java. Someone should try to standardise the
terminology a bit more fully.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
- Karri Kalpio
 
E

Eric Sosman

Joona said:
I write Java code for a living, and a colleague of mine (who works in
the same Java code project) keeps saying "function" when he means
"method". This is absurd - C functions and Java methods are
conceptually the same thing, but you have to call them "functions" in
C and "methods" in Java. Someone should try to standardise the
terminology a bit more fully.

Answer #1: ISO/IEC 9899:1999 calls them "functions."

Answer #2: Good idea. Shall we standardize on "function,"
"method," "procedure," "subroutine," or "word?" Personally,
I favor "lambda expression" because throwing Greek letters
around deceives people into thinking I'm smarter than I am.
"Method" won't get me a raise, but "lambda expression" (so
long as I'm careful not to say "lambada expression") may.
 
C

Chris Croughton

I write Java code for a living, and a colleague of mine (who works in
the same Java code project) keeps saying "function" when he means
"method". This is absurd - C functions and Java methods are
conceptually the same thing, but you have to call them "functions" in
C and "methods" in Java. Someone should try to standardise the
terminology a bit more fully.

They aren't the same thing, exactly. A 'method' is a special type of
function which applies only to an 'object' (and within that method the
object under consideration is normally available implicitly). In C++
are both ordinary functions and methods:

class FRED
{
public:
void func1(int x)
{
xxx = x;
}
int xxx;
};

void func1(FRED *f, int x)
{
f.xxx = x;
}

func1 is a method of class FRED, and so a pointer to the object being
used is passed in implicitly (as *this), func2 is an external function
which takes a pointer to type FRED as an explicit argument.

In particular, they are called in different ways:

int main()
{
FRED a, b;
a.func1(1); // calling the method
b.func1(2) // calling the method
func2(&a); // calling the function
func2(&b); // calling the function
}

The confusion is that methods look like external functions, apart from
the lack of an explicit pointer to a class...

(I believe that in Java everything is part of an object, so they are all
methods...)

Chris C
 
E

Emmanuel Delahaye

JS wrote on 22/03/05 :
Is it only allowed to make a pointer point to something in a method??

If you meant a function, yes.
#include <stdio.h>

struct data {

int x;

int y;

};

struct data test, *pp;

pp = &test; //WHY IS THIS ILLEGAL??

main(){

pp = &test;

}

Why is the above code illegal??

Because a statement can only occur in a bloc. It's a C rule. Don't ask
me why is it so.

BTW, this is correct:

struct data test, *pp = &test;

because it's not a statement, it's an initialization. It occurs before
main() is called.

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

"C is a sharp tool"
 
E

Emmanuel Delahaye

JS wrote on 23/03/05 :
Thats another thing that confuses me why I need to use "," before *pp.
I have made this in my code.

struct data test *pp;

but will only compile if I write:

struct data test, *pp;

This is because you are defining two objects of the same type on the
same line. The C language requires a separator, anf it happens to be
the comma (,).

struct data test, *pp;

is a shortform for

struct data test;
struct data *pp;

that has my preference for various reasons.

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

"C is a sharp tool"
 
E

Emmanuel Delahaye

Joona I Palaste wrote on 23/03/05 :
I write Java code for a living, and a colleague of mine (who works in
the same Java code project) keeps saying "function" when he means
"method". This is absurd - C functions and Java methods are
conceptually the same thing, but you have to call them "functions" in
C and "methods" in Java. Someone should try to standardise the
terminology a bit more fully.

What for? Different languages, different words, different cultures...
That's fine. I don't believe into the Programming Language Esperanto...
I'm fine with bio-diversity...

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

..sig under repair
 
C

CBFalconer

Joona said:
I write Java code for a living, and a colleague of mine (who works
in the same Java code project) keeps saying "function" when he
means "method". This is absurd - C functions and Java methods are
conceptually the same thing, but you have to call them "functions"
in C and "methods" in Java. Someone should try to standardise the
terminology a bit more fully.

functions were here first. :) methods have a hidden 'this'.
 

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

Forum statistics

Threads
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top