Considering the following line of code:
int *ip;
Is "ip is a pointer to int" (the type) or is "ip a pointer to an int" (a particular variable of type int)?
Thanks.
Considering the following line of code:
int *ip;
Is "ip is a pointer to int" (the type) or is "ip a pointer to an int" (a particular variable of type int)?
Thanks.
ip is just an int pointer because it has not been set to point to a particular object. int x = 1; int* ip = &x;
is a pointer to a variable of type int. ip could also be a pointer to an array of ints
int nums[3] = {1,2,3};
int* ip = nums;
int* ip = &nums[0]; // This is another way of saying the above line
Hi dotancohen,
The
int *ip;
is just a declaration of an int type pointer. It has not been assigned to point to any memory location yet. A pointer of type int can only point to the memory address of an integer variable e.g
int *ip,dc;//Declaration of int type pointer *ip and int variable dc. Please note that int* ip and int *ip mean the same thing.
dc=1000;//initialization of variable dc to 1000
ip=&dc;//Now pointer ip points to the memory address of int dc
Regards.
Is "ip is a pointer to int" (the type) or is "ip a pointer to an int" (a particular variable of type int)?
If you want to get specific, "ip is an object with the type pointer to int". An object has a type. The object is a concrete entity that can be worked with at runtime, and its type is a blueprint for the appearance and behavior of an object. There can be many objects of a type, but an object can only have one type (at any given time).
Pointers are slightly confusing because there are multiple levels of objects. You have the pointer object which stores an address to an object of the pointed to type. For a single pointer, there are two objects at work. For a double pointer there are three (and so on):
int i = 22; /* i is an int */
int *p = &i; /* p is a pointer to int */
int **pp = &p; /* pp is a pointer to pointer to int */
pp (int **) <some address>
|
+---> p (int *) <some address>
|
+---> i (int) 22
p.s. How about, "ip is a symbol for the memory location declared to store an object with the type pointer to int"? ;)
This code
int *ip;
means that you declare a pointer that is going to pointing to a place of memory that contain integer value notice that you don't point anywhere until you set value for your pointer.if you want to change value of the pointer you should use * before pointer name but if you want to tell the compiler that this pointer point to a place of memory you should not use * before that.
int *ip;
int place;
ip = place // from now the ip point to where variable place is stored
*ip = 5; //means that the value of ip is 5
so both of your statements seems true depend on you set value for your pointer or not
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.