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?? 18 1763
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. }
In article <d1**********@n ews.net.uni-c.dk>, JS <dsa.@asdf.co m> 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.
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
"JS" <dsa.@asdf.co m> 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_Keit h) 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.
Mark Odell <od*******@hotm ail.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_Keit h) 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.
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 ---
"Walter Roberson" <ro******@ibd.n rc-cnrc.gc.ca> skrev i en meddelelse
news:d1******** **@canopus.cc.u manitoba.ca... In article <d1**********@n ews.net.uni-c.dk>, JS <dsa.@asdf.co m> 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;
JS <dsa.@asdf.co m> wrote: "Walter Roberson" <ro******@ibd.n rc-cnrc.gc.ca> skrev i en meddelelse news:d1******** **@canopus.cc.u manitoba.ca... In article <d1**********@n ews.net.uni-c.dk>, JS <dsa.@asdf.co m> 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***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
"Keith Thompson" <ks***@mib.or g> 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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 sure what all the right
terminology is.) A code example should make it clear what I'm asking:
class A
{
public:
|
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 agree on the types for
the methods.
In the code below, the constructor to NLF2 (near the bottom) takes as
its last two arguments two function pointers of type void(*)(...), and
apparently &PR<2>::PR_init is of type void(PR<2>::*)(...).
If I...
|
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
}
|
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, the derived classes have methods (not available on the base
class) that I would like to invoke. I thought I could simply cast the
pointer to the appropriate derived class and access the methods this way
- but that dosen't work.
|
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 the details, but does someone have a clue of what
this person perhaps was talking about? Do you think he just ment
something such as function pointers? Or could I, much like a C++
Class, call a class function that has access to the structure...
| |
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 way to take the
pointers (again, IntPtr I assume?) into strings?
Thanks,
Bob
|
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?
Thanks,
Bahadir
|
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;
}
|
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 values sounds
ideal.
I would like to know if the usage of this function pointer is valid
before I refactor some of the other functions that use the same data
structure to complete functions.
class Object
|
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
vector_static_function.c:21: error: expected constructor, destructor,
or type conversion before '.' token
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |