int** in c++ to objective c - c++

what means the two stars in this c++ code, I try to convert it in objective c but I never saw that how can I do that?
int **gFFTBitTable = NULL;
Thanks for advance

In both Objective C and C++ it is valid. It is pointer to pointer. As, pointer can keep address of other variable, pointer to pointer can keep address of pointer.
Example,
int i;
int *p = &i; //p is pointer
int **q = &p; //q is pointer to pointer

It means it's a pointer of pointers. If a pointer can be used like an array to keep track of say 10 integers (a row) a pointer of pointers could keep track of say 10 of those pointers (a table)

Related

c++ Pointer of pointer gives a value?

I am wondering one thing, I am trying to remember c++ after long working with c# :)
And one thing bugs me, and I don't know why it is happening.
Pointer should be thing, which points somewhere to memory(it's address)
And pointer of pointer is address of the pointer. Simple, right? :D
Let's take a look at this code:
#include <iostream>
int main()
{
int* TestMas = new int[10]
{
1,2,3,4,5,6,7,8,9
};
for (size_t i = 0; i < 10; i++)
{
printf("%d\t", *(TestMas+i));
}
}
For some reason, values are printed out instead of the addresses, it should be *(TestMas+i) should be address of the array pointer, but how values are printed out, and not address?
If I try to use & to dereference the pointer, I get an error xD How is it working?
TestMas is a int *, or the address of an integer. When you assign it to new int[10], you're pointing it to the first of 10 contiguous integers in memory that you guarantee belong to you.
So when you do *(TestMas + i), you're doing pointer arithmetic, and then dereferencing the new pointer. Note that the syntactic sugar for exactly this operation would be TestMas[i].
You probably meant to leave off the dereference if you just wanted the address. So just:
std::cout << TestMas + i;
TestMas is a pointer to an int, not a pointer to a pointer. Its type is int *. A pointer to a pointer to an int would be int **
TestMas + i is also a pointer to an int, pointing to the i-th element of the array. *(TestMas + i) is calling the indirection operator on that pointer, which returns the object or function pointed-to by the pointer operand, not the address.
The address is the value of (TestMas+i) itself.
You can print it with:
printf("%p\t", (TestMas+i) );

What exactly is the purpose of the (asterisk) in pointers?

I'm new to programming and I'm trying to wrap my head around the idea of 'pointers'.
int main()
{
int x = 5;
int *pointerToInteger = & x;
cout<<pointerToInteger;
}
Why is it that when I cout << pointerToInteger; the output is a hexdecimal value, BUT when I use cout << *pointerToInteger; the output is 5 ( x=5).
* has different meaning depending on the context.
Declaration of a pointer
int* ap; // It defines ap to be a pointer to an int.
void foo(int* p); // Declares function foo.
// foo expects a pointer to an int as an argument.
Dereference a pointer in an expression.
int i = 0;
int* ap = &i; // ap points to i
*ap = 10; // Indirectly sets the value of i to 10
A multiplication operator.
int i = 10*20; // Needs no explanation.
One way to look at it, is that the variable in your source/code, say
int a=0;
Makes the 'int a' refer to a value in memory, 0. If we make a new variable, this time a (potentially smaller) "int pointer", int *, and have it point to the &a (address of a)
int*p_a=&a; //(`p_a` meaning pointer to `a` see Hungarian notation)
Hungarian notation wiki
we get p_a that points to what the value &a is. Your talking about what is at the address of a now tho, and the *p_a is a pointer to whatever is at the &a (address of a).
This has uses when you want to modify a value in memory, without creating a duplicate container.
p_a itself has a footprint in memory however (potentially smaller than a itself) and when you cout<<p_a<<endl; you will write whatever the pointer address is, not whats there. *p_a however will be &a.
p_a is normally smaller than a itself, since its just a pointer to memory and not the value itself. Does that make sense? A vector of pointers will be easier to manage than a vector of values, but they will do the same thing in many regards.
If you declare a variable of some type, then you can also declare another variable pointing to it.
For example:
int a;
int* b = &a;
So in essence, for each basic type, we also have a corresponding pointer type.
For example: short and short*.
There are two ways to "look at" variable b (that's what probably confuses most beginners):
You can consider b as a variable of type int*.
You can consider *b as a variable of type int.
Hence, some people would declare int* b, whereas others would declare int *b.
But the fact of the matter is that these two declarations are identical (the spaces are meaningless).
You can use either b as a pointer to an integer value, or *b as the actual pointed integer value.
You can get (read) the pointed value: int c = *b.
And you can set (write) the pointed value: *b = 5.
A pointer can point to any memory address, and not only to the address of some variable that you have previously declared. However, you must be careful when using pointers in order to get or set the value located at the pointed memory address.
For example:
int* a = (int*)0x8000000;
Here, we have variable a pointing to memory address 0x8000000.
If this memory address is not mapped within the memory space of your program, then any read or write operation using *a will most likely cause your program to crash, due to a memory access violation.
You can safely change the value of a, but you should be very careful changing the value of *a.
yes the asterisk * have different meanings while declaring a pointer variable and while accessing data through pointer variable. for e.g
int input = 7;
int *i_ptr = &input;/*Here * indicates that i_ptr is a pointer variable
Also address is assigned to i_ptr, not to *iptr*/
cout<<*i_ptr;/* now this * is fetch the data from assigned address */
cout<<i_ptr;/*it prints address */
for e.g if you declare like int *ptr = 7; its wrong(not an error) as pointers ptr expects valid address but you provided constant(7). upto declaration it's okay but when you go for dereferencing it like *ptr it gives problem because it doesn't know what is that data/value at 7 location. So Its always advisable to assign pointers variable with valid addresses. for e.g
int input = 7;
int *i_ptr = &input;
cout<<*i_ptr;
for example
char *ptr = "Hello"; => here * is just to inform the compiler that ptr is a pointer variable not normal one &
Hello is a char array i.e valid address, so this syntax is okay. Now you can do
if(*ptr == 'H') {
/*....*/
}
else {
/*.... */
}

Get a copy of the pointer to an array in c++

Hi I am learning C++ Primer and have a simple question. Please help me out.
So I know that "int *p = (*q)[10]" where p is a pointer to an array of 10. The question is, in a function definition: "void print(int (*q)[10])" where this pointer is a parameter. how could I get the copy of that pointer. Will that be "int *p = q"?
I technically don't know how to test my correctness in C++.
Thanks
int (*q)[10]
declares q as a pointer to array of 10 ints. Only arrays decay to pointers to their underlying type, but pointers to arrays do not decay to pointers to underlying type, so trying int* p = q; will result in a compilation error. What you need is
int (*p)[10] = q; // copies the pointer q to p

Pointers to Pointers (C++)

Ok I understand the concept of pointers to pointers. But I don't know why this doesn't work.
void function(int **x)
{
*x = 10;
}
I keep getting the error:
a value of type "int" cannot be assigned to an entity of type "int*"
What am I doing wrong or what am I not understanding about pointers to pointers?
omg x_x I was confusing C with C++.
x is a pointer to a pointer so you have to dereference it twice to get to the actual object. E.g. **x = 10;
Ok I understand the concept of pointers to pointers.
Nah...
*x is an int*, so you can't assign an int to it.
The concept of pointers-to-pointers comes from C, where references aren't available. It allows reference semantics - i.e. you can change the original pointer.
In short, you need to dereference twice.
A dereference returns the thing a pointer is ponting to, so:
int n = 10; //here's an int called n
int* pInt = &n; //pInt points to n (an int)
int** ppInt = &pInt //ppInt points to pInt (a pointer)
cout << ppInt; //the memory address of the pointer pInt (since ppInt is pointing to it)
cout << *ppInt; //the content of what ppInt is pointing to (another memory address, since ppInt is pointing to another pointer
cout << *pInt; //the content of what pInt is pointing to (10)
cout << **ppInt; //the content of what the content of ppInt is pointing to (10)
x is pointing to a type of int *, not int. You either want to do static int i = 10; *x = &i or **x = 10.

C++ pointers - What values are held here?

If I have the following pointer variable declarations:
int *a;
int **c;
Regarding to the type and what values each will hold, will it be as follows:
a is of type int*, and will hold a memory address
*a is of type int, and will hold the value of the variable the pointer is pointing to
c is of type int**, and will hold ?????????????????????
c* is of type int*, and will hold the memory address of the pointer it is pointing at
c** is of type int, and assuming that pointer c is pointing to pointer b, and pointer b is pointing to variable a, here, the value held will be the value of the variable a
Is it correct this way, except c which I'm not sure about?
Thanks.
int *a;
int **c;
You are correct about a. It is more common to say a holds a pointer to int.
c is of type int** and will hold a pointer to a pointer to int.
*c is of type int*, and will hold a pointer to int.
**c is of type int, and will hold an integer value. You are correct about c pointing to b and b pointing to a.
See cdecl for some help. :)
c is of type int**, and will hold ?????????????????????
'c' also holds a memory address, just as 'a' does. The difference is that 'c', when dereferenced, will return another memory address. You're just adding another level of indirection.
In your example, c will hold a pointer to an int*; that is, c is a pointer to a pointer. It can be used for multidimensional arrays (like matrices) and it can be used as a function parameter to change a user's int*.
-- a is of type int*, and will hold a memory address
Correct
-- *a is of type int, and will hold the value of the variable the pointer is pointing to
Not exactly: *a will be a reference to the variable the adress points to. You see this when trying *a = 8; and int * x = &(*a)).
If it was a value you couldn't change it. But since it is a reference the value is "routed" to the original place...in this case the memory a points to.
-- c is of type int**, and will hold ?????????????????????
c holds a memory address pointing to a memory address pointing to an int.
*c holds a reference to a memory adress pointing to an int. So you can do: *c = a;
**c is the same as *a.
Every pointer holds memory address. In this case, c is a pointer to int* so, it will hold a memory address of such a variable.
Often, double pointers are used to create dynamic multiarrays in C. You can see it here
C is of type int*. As evident from that, it will hold a data of type int* which in itself is a memory address.
This concept is called far pointer and there can be multiple far pointers up to a certain limit.
Like int** c, you can also have int*** d point to int** c. This is like a queue with every pointer pointing to the next pointer and the front having the data as the actual variable.
The simplest thing that I can think of is playing with spaces. The compiler does not really care, but it makes reasoning easier due to the fact that the syntax of declarations and the usage are the same (by design):
Given:
int ***a; // declaration
int*** a; // type of a is an int*** (read: pointer to pointer to pointer to int)
int** *a; // the type of the object pointed by a (*a) is int**
int* **a; // the type of **a is int*
int ***a; // the type of ***a is int
Disclaimer: this is with respect to data types. Whether you can or not dereference a pointer at runtime is a different issue (has it been initialized, does it point to valid memory...?)