473,464 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointers in methods??

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??
Nov 14 '05 #1
18 1744
JS wrote:
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.

}

Nov 14 '05 #2
In article <d1**********@news.net.uni-c.dk>, JS <dsa.@asdf.com> wrote:
: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;

--
Feep if you love VT-52's.
Nov 14 '05 #3


JS wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #4
"JS" <dsa.@asdf.com> writes:
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;
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
Mark Odell <od*******@hotmail.com> writes:
JS wrote:
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.
}


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
JS wrote:
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?

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #7
JS

"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.ca> skrev i en meddelelse
news:d1**********@canopus.cc.umanitoba.ca...
In article <d1**********@news.net.uni-c.dk>, JS <dsa.@asdf.com> wrote:
: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;
Nov 14 '05 #8
JS <dsa.@asdf.com> wrote:
"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.ca> skrev i en meddelelse
news:d1**********@canopus.cc.umanitoba.ca...
In article <d1**********@news.net.uni-c.dk>, JS <dsa.@asdf.com> wrote:
: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;


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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #9
"Keith Thompson" <ks***@mib.org> wrote in message

<snip>
#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?"

--
Tor <torust AT online DOT no>

Nov 14 '05 #10
Eric Sosman <er*********@sun.com> scribbled the following:
JS wrote:
Is it only allowed to make a pointer point to something in a method??

(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 (pa*****@cc.helsinki.fi) ------------- 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
Nov 14 '05 #11


Joona I Palaste wrote:
Eric Sosman <er*********@sun.com> scribbled the following:
JS wrote:
Is it only allowed to make a pointer point to something in a method??

(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.


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.

--
Er*********@sun.com

Nov 14 '05 #12
On 23 Mar 2005 17:07:32 GMT, Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
Eric Sosman <er*********@sun.com> scribbled the following:
JS wrote:
Is it only allowed to make a pointer point to something in a method??


(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.


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
Nov 14 '05 #13
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"

Nov 14 '05 #14
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"

Nov 14 '05 #15
Joona I Palaste wrote on 23/03/05 :
Eric Sosman <er*********@sun.com> scribbled the following:
JS wrote:
Is it only allowed to make a pointer point to something in a method??


(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.


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

Nov 14 '05 #16
Tor Rustad wrote on 23/03/05 :
I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"


Copied into my .sig file if you don't mind!

--
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

Nov 14 '05 #17
Joona I Palaste wrote:
Eric Sosman <er*********@sun.com> scribbled the following:
JS wrote:

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


(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.


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

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #18
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
Tor Rustad wrote on 23/03/05 :

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


Copied into my .sig file if you don't mind!


Not at all! :-)

--
Tor <torust AT online DOT no>

Nov 14 '05 #19

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

Similar topics

4
by: Yu Hu | last post by:
Hello, Is it legal (and moral) to assign a method pointer with the proper prototype to a variable whose type involves the superclass? (Sorry if my wording of the question is imprecise, I'm not...
1
by: Martin Magnusson | last post by:
I have a partially specialized templated class, which at one point needs to pass pointers to some of its methods to another function. However, it seems that my compiler (gcc) and me don't quite...
8
by: He Shiming | last post by:
Hi, I've developed a class that implements an interface definition. It looks like this: class IRecord { public: // define interface methods by pure virtual methods // no member variables }
4
by: Alfonso Morra | last post by:
Hi, I have a variable that stores a pointer to a base class. I am using this variable to store pointers to objects of the base class, as well as pointers to other derived classes. However,...
10
by: gmtonyhoyt | last post by:
It's been mentioned to me that, in standard c, that you can have structures behave very much like classes in relation to something to do with function calls within a structure. Now, I didn't get...
2
by: Bob Dankert | last post by:
I need to use functions through PInvoke which accept and return pointers to strings. What is the best way to get string pointers (IntPtr I am guessing?) to pass into these functions, and the best...
3
by: Bilgehan.Balban | last post by:
Hi, How do I declare an array of function pointers and assign each element of the array with public member functions of a class? Is it possible that the array is not a member of the class? ...
7
by: Erdal Mutlu | last post by:
Hi, I am trying to design a base class (interface) with two or more subclasses as follows: class A { .... virtual static A* getByName(const string x)=0 const; }
1
by: Bushido Hacks | last post by:
A private utility function using a function pointer sounds ideal to me. I want to simpify writing the same set of loops. While there are a few functions that can't use it, setting and modifying...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.