#include <iostream>
#include <cmath>
using namespace std;
struct demo{
int one;
int two;
int three;
};
int main()
{
demo d1;
demo *dptr=&d1;
*dptr=1 ;
++dptr;
*dptr=2;
++dptr;
*dptr=3;
return 0;
}
Please explain why the above code looks logical but in fact does not work
in line 13 of code. Log error:
no match for 'operator=' in '*dptr=1'
demo d1;
demo *dptr=&d1;
*dptr=1 ;
++dptr;
dptr=2;
++dptr;
dptr=3;
dptr is a pointer pointing to a demo struct. So, *dptr = 1 is basically the same as d1 = 1;, that's not valid.
Plus, having a pointer of that type and doing ++ on that pointer applies pointer arithmetic for that type, shoving the pointer sizeof(demo), that's not what you want here. You'll need to create an int pointer by casting it, then using that pointer to read the 3 fields
int* dptr=reinterpret_cast<int*>(&d1);
Padding can still ruin your day though, however, since they're all int's you should be fine.
You need to declare your dptr as int*, not demo*.
int *dptr=&d1; // might need type cast (int*)
*dptr=1 ;
Related
What is the difference between int* i and int** i?
Pointer to an integer value
int* i
Pointer to a pointer to an integer value
int** i
(Ie, in the second case you will require two dereferrences to access the integer's value)
int* i : i is a pointer to a object of type int
int** i : i is a pointer to a pointer to a object of type int
int*** i : i is a pointer to a pointer to a pointer to object of type int
int**** i : i is a pointer to a pointer to a pointer to a pointer to object of type int
...
int* pi
pi is a pointer to an integer
int **ppi
ppi is a pointer to a pointer to an integer.
EDIT :
You need to read a good book on pointers. I recommend Pointers on C by Kenneth Reek.
Let's say you're a teacher and have to give notes to one of your students.
int note;
Well ... I meant the whole class
int *class_note; /* class_note[0]: note for Adam; class_note[1]: note for Brian; ... */
Well ... don't forget you have several classes
int **classes_notes; /* classes_notes[0][2]: note for Charles in class 0; ... */
And, you also teach at several institutions
int ***intitute_note; /* institute_note[1][1][1]: note for David in class 1 of institute 1 */
etc, etc ...
I don't think this is specific to opencv.
int *i is declaring a pointer to an int. So i stores a memory address, and C is expecting the contents of that memory address to contain an int.
int **i is declaring a pointer to... a pointer. To an int. So i contains an address, and at that memory address, C is expecting to see another pointer. That second memory address, then, is expected to hold an int.
Do note that, while you are declaring a pointer to an int, the actual int is not allocated. So it is valid to say int *i = 23, which is saying "I have a variable and I want it to point to memory address 23 which will contain an int." But if you tried to actually read or write to memory address 23, you would probably segfault, since your program doesn't "own" that chunk of RAM. *i = 100 would segfault. (The solution is to use malloc(). Or you can make it point to an existing variable, as in int j = 5; int *i = &j)
Imagine you have a few friends, one of them has to give you something (a treasure... :-)
Say john has the treasure
int treasure = 10000; // in USD, EUR or even better, in SO rep points
If you ask directly john
int john = treasure;
int you = john;
If you cannot join john, but gill knows how to contact him,
int john = treasure;
int *gill = &john;
int you = *gill;
If you cannot even join gill, but have to contact first jake who can contact gill
int john = treasure;
int *gill = &john;
int **jake = &gill;
int you = **jake;
Etc... Pointers are only indirections.
That was my last story for today before going to bed :-)
I deeply believe that a picture is worth a thousand words. Take the following example
// Finds the first integer "I" in the sequence of N integers pointed to by "A" .
// If an integer is found, the pointer pointed to by P is set to point to
// that integer.
void f(int N, int *A, int I, int **P) {
for(int i = 0; i < N; i++)
if(A[i] == I) {
// Set the pointer pointed to by P to point to the ith integer.
*P = &A[i];
return;
}
}
So in the above, A points to the first integer in the sequence of N integers. And P points to a pointer that the caller will have the pointer to the found integer stored in.
int Is[] = { 1, 2, 3 };
int *P;
f(3, &Is[0], 2, &P);
assert(*P == 2);
&P is used to pass the address of P to the function. This address has type int **, because it's the address of a pointer to int.
int* i is the address of a memory location of an integer
int** is the address of a memory location of an address of a memory location of an integer
int* i; // i is a pointer to integer. It can hold the address of a integer variable.
int** i; // i is a pointer to pointer to integer. It can hold address of a integer pointer variable.
Neither is a declaration. Declaration syntax does not allow () around the entire declaration. What are these () doing there? If this is supposed to be a part of function declaration, include the whole function declaration thing in your question, since in general case the actual meaning of a declaration might depend on that. (Not in this one though.)
As for the difference... There is one * in the first and there are two *s in the second. Does it help? Probably not. The first one declares ias a pointer to int. The second one declares i as a pointer to int *. Does this help? Probably not much either. Without a more specific question, it is hard to provide a more meaningful answer.
Provide more context, please. Or, if this is actually as specific as it can get, read your favorite C or C++ book about pointers. Such broad generic questions is not something you ask on the net.
Note that
int *i
is not fully interchangeable with
int i[]
This can be seen in that the following will compile:
int *i = new int[5];
while this will not:
int i[] = new int[5];
For the second, you have to give it a constructor list:
int i[] = {5,2,1,6,3};
You also get some checking with the [] form:
int *i = new int[5];
int *j = &(i[1]);
delete j;
compiles warning free, while:
int i[] = {0,1,2,3,4};
int j[] = {i[1]};
delete j;
will give the warnings:
warning C4156: deletion of an array expression without using the array form of 'delete'; array form substituted
warning C4154: deletion of an array expression; conversion to pointer supplied
Both of these last two examples will crash the application, but the second version (using the [] declaration type) will give a warning that you're shooting yourself in the foot.
(Win32 console C++ project, Visual studio 2010)
Textual substitution is useful here, but beware of using it blindly as it can mislead you (as in the advanced example below).
T var; // var has type T
T* var; // var has type "pointer to T"
This works no matter what T is:
int* var; // pointer to int
char* var; // pointer to char
double* var; // pointer to double
// advanced (and not pure textual substitution):
typedef int int3[3]; // confusing: int3 has type "array (of size 3) of ints"
// also known as "int[3]"
int3* var; // pointer to "array (of size 3) of ints"
// aka "pointer to int[3]"
int (*var)[3]; // same as above, note how the array type from the typedef
// gets "unwrapped" around the declaration, using parens
// because [] has higher precedence than *
// ("int* var[3];" is an array (size 3) of pointers to int)
This works when T is itself a pointer type:
typedef int* T; // T is a synonym for "pointer to int"
T* var; // pointer to T
// which means pointer to pointer to int
// same as:
int** var;
Header:
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
int i[8];
#endif
Main:
#include <iostream>
#include <string>
#include "header.h"
using namespace std
int main(){
int *test;
string bits = "10011011";
*test = func(bits); // ERROR 2 HERE
//my goal here is to have a pointer in main that
//points to the 1 address of the global var array i
} //if im totally missing the point and there is a
//better way to do this please let me know
Function:
#include <iostream>
#include <string>
using namespace std
int *func(string str){
int l = str.size();
int *ptr;
for(int k=0; k < l; ++k){
i[k] = s[k] - '0';
}
*ptr = i; // ERROR 1 HERE
return ptr;
}
Hi all,
When I attempt to compile the above code, I get two errors as labeled. They are:
Error 1: invalid conversiopn from int* to int
Error 2: invalid conversion from int to *int
It seems I must have a fundamental understanding of what I am working with here. Am I not setting *ptr to point at the first memory address of the array i[]? Why is the compiler telling me that I am trying to set the value of the pointer to the value of i[]? I want to set the value of the pointer to the ADDRESS of i[]. If I add an & I get the same error.
Error 2 obviously follows error 1. It is the same error, just backwards.
My question is, then, what the heck am I doing wrong when I am trying to point *ptr in func() at the array i[]? I DID try finding the answers elsewhere to no avail.
Thanks.
edit: this code is a rough transcription of the original (from another PC), so if you try to compile it and there are typos I apologize.
int *ptr; declares a pointer to int, so ptr is a pointer: a variable that stores the memory location of some int. Currently it doesn't point to anything in particular, just some random place in memory that you probably don't own.
*ptr dereferences ptr, so *ptr is the int that ptr points to.
When you do *ptr = i; you are attempting to set the int pointed to by ptr equal to i, which is an array of 8 ints. In this context, the array decays into a pointer to its first element, so you're attempting to set an int equal to an int*, hence the error.
This should just be ptr = i;, but even that isn't necessary, since you could just return i; and i would decay into a pointer to the first element of the array.
Similarly, *test = func(bits); attempts to set the int pointed to by test equal to the pointer returned by func. It should just be test = func(bits);, in which case the address that test contains will become the one that func returned. In this case test will point to the first element of i.
I read code of mysql source code in line 148, source code link here
I got confused here:
typename List::node_type& elem_node = elem->*list.node;
It's so strange: ->*.
What's the grammar meaning?
The ->* notation is needed because list is a pointer to a member. Here's a simple example:
#include <iostream>
struct S {
int a;
int b;
};
int main()
{
// 'ptr' now points to the 'b' member of some 'S' value.
int (S::*ptr) = &S::b;
S *x = new S;
x->*ptr = 5;
std::cout << x->b << '\n';
}
This program prints '5'. The ptr pointer is made to be of type int (S::*), i.e. a pointer to some int member of a S struct. It's initialized to point to the b member.
Of course, to actually do something sensible with it you need to specify which S struct to modify. That's done via
x->*ptr
Which can be read as "the member of the x value which is dereferenced by the ptr pointer".
I wrote two programs which prints out the variable the pointer p points to:
First program:
#include <stdio.h>
int main (void){
int *p;
int a=5, q;
p=&a;
q=*p;
printf("%d", q);
}
Second program:
#include <stdio.h>
int main(void)
{
int a=5;
int*p;
p= (int*)&a;
printf("%d", *p);
return 0;
}
My question:
Both the programs print the value of a which is 5. However, the second program uses p=(int*)&a; instead of just p=&a;. Could someone please tell me the significance of (int*) casting here?
Casting is a way for a programmer to tell the computer that, even though the computer thinks something is one type, we want to treat it as another type.
But here the cast is of no use as here a is an integer and thus address of a will need no cast here for integer pointer.
There is no significance, rather, this cast is superfluous and not required.
In your code, a is of type int, p is of type int *. Hence,
p= (int*)&a;
and
p= &a;
are same and the second one here is recommended.
It's useless.
a is an int, so &a is already a pointer to int.
Useless use of type casting. It's like,
int a = (int) 10 ;
That is typecasting ,to tell the compiler that the value that is assigned to the pointer p that is &a is an integer type(int) but there it is of no use as &a return the address that is assigned to the variable a and it is always an integer.
What is the difference between int* i and int** i?
Pointer to an integer value
int* i
Pointer to a pointer to an integer value
int** i
(Ie, in the second case you will require two dereferrences to access the integer's value)
int* i : i is a pointer to a object of type int
int** i : i is a pointer to a pointer to a object of type int
int*** i : i is a pointer to a pointer to a pointer to object of type int
int**** i : i is a pointer to a pointer to a pointer to a pointer to object of type int
...
int* pi
pi is a pointer to an integer
int **ppi
ppi is a pointer to a pointer to an integer.
EDIT :
You need to read a good book on pointers. I recommend Pointers on C by Kenneth Reek.
Let's say you're a teacher and have to give notes to one of your students.
int note;
Well ... I meant the whole class
int *class_note; /* class_note[0]: note for Adam; class_note[1]: note for Brian; ... */
Well ... don't forget you have several classes
int **classes_notes; /* classes_notes[0][2]: note for Charles in class 0; ... */
And, you also teach at several institutions
int ***intitute_note; /* institute_note[1][1][1]: note for David in class 1 of institute 1 */
etc, etc ...
I don't think this is specific to opencv.
int *i is declaring a pointer to an int. So i stores a memory address, and C is expecting the contents of that memory address to contain an int.
int **i is declaring a pointer to... a pointer. To an int. So i contains an address, and at that memory address, C is expecting to see another pointer. That second memory address, then, is expected to hold an int.
Do note that, while you are declaring a pointer to an int, the actual int is not allocated. So it is valid to say int *i = 23, which is saying "I have a variable and I want it to point to memory address 23 which will contain an int." But if you tried to actually read or write to memory address 23, you would probably segfault, since your program doesn't "own" that chunk of RAM. *i = 100 would segfault. (The solution is to use malloc(). Or you can make it point to an existing variable, as in int j = 5; int *i = &j)
Imagine you have a few friends, one of them has to give you something (a treasure... :-)
Say john has the treasure
int treasure = 10000; // in USD, EUR or even better, in SO rep points
If you ask directly john
int john = treasure;
int you = john;
If you cannot join john, but gill knows how to contact him,
int john = treasure;
int *gill = &john;
int you = *gill;
If you cannot even join gill, but have to contact first jake who can contact gill
int john = treasure;
int *gill = &john;
int **jake = &gill;
int you = **jake;
Etc... Pointers are only indirections.
That was my last story for today before going to bed :-)
I deeply believe that a picture is worth a thousand words. Take the following example
// Finds the first integer "I" in the sequence of N integers pointed to by "A" .
// If an integer is found, the pointer pointed to by P is set to point to
// that integer.
void f(int N, int *A, int I, int **P) {
for(int i = 0; i < N; i++)
if(A[i] == I) {
// Set the pointer pointed to by P to point to the ith integer.
*P = &A[i];
return;
}
}
So in the above, A points to the first integer in the sequence of N integers. And P points to a pointer that the caller will have the pointer to the found integer stored in.
int Is[] = { 1, 2, 3 };
int *P;
f(3, &Is[0], 2, &P);
assert(*P == 2);
&P is used to pass the address of P to the function. This address has type int **, because it's the address of a pointer to int.
int* i is the address of a memory location of an integer
int** is the address of a memory location of an address of a memory location of an integer
int* i; // i is a pointer to integer. It can hold the address of a integer variable.
int** i; // i is a pointer to pointer to integer. It can hold address of a integer pointer variable.
Neither is a declaration. Declaration syntax does not allow () around the entire declaration. What are these () doing there? If this is supposed to be a part of function declaration, include the whole function declaration thing in your question, since in general case the actual meaning of a declaration might depend on that. (Not in this one though.)
As for the difference... There is one * in the first and there are two *s in the second. Does it help? Probably not. The first one declares ias a pointer to int. The second one declares i as a pointer to int *. Does this help? Probably not much either. Without a more specific question, it is hard to provide a more meaningful answer.
Provide more context, please. Or, if this is actually as specific as it can get, read your favorite C or C++ book about pointers. Such broad generic questions is not something you ask on the net.
Note that
int *i
is not fully interchangeable with
int i[]
This can be seen in that the following will compile:
int *i = new int[5];
while this will not:
int i[] = new int[5];
For the second, you have to give it a constructor list:
int i[] = {5,2,1,6,3};
You also get some checking with the [] form:
int *i = new int[5];
int *j = &(i[1]);
delete j;
compiles warning free, while:
int i[] = {0,1,2,3,4};
int j[] = {i[1]};
delete j;
will give the warnings:
warning C4156: deletion of an array expression without using the array form of 'delete'; array form substituted
warning C4154: deletion of an array expression; conversion to pointer supplied
Both of these last two examples will crash the application, but the second version (using the [] declaration type) will give a warning that you're shooting yourself in the foot.
(Win32 console C++ project, Visual studio 2010)
Textual substitution is useful here, but beware of using it blindly as it can mislead you (as in the advanced example below).
T var; // var has type T
T* var; // var has type "pointer to T"
This works no matter what T is:
int* var; // pointer to int
char* var; // pointer to char
double* var; // pointer to double
// advanced (and not pure textual substitution):
typedef int int3[3]; // confusing: int3 has type "array (of size 3) of ints"
// also known as "int[3]"
int3* var; // pointer to "array (of size 3) of ints"
// aka "pointer to int[3]"
int (*var)[3]; // same as above, note how the array type from the typedef
// gets "unwrapped" around the declaration, using parens
// because [] has higher precedence than *
// ("int* var[3];" is an array (size 3) of pointers to int)
This works when T is itself a pointer type:
typedef int* T; // T is a synonym for "pointer to int"
T* var; // pointer to T
// which means pointer to pointer to int
// same as:
int** var;