Why is my pointer giving back value at address directly? - c

Here is my code:
typedef struct {
int** matrix;
int rows;
int cols;
}intMat;
intMat create_intMat(int rows,int cols,int matrix[rows][cols]){
intMat A;
A.rows = rows;
A.cols = cols;
int** mat;
mat = malloc(A.rows*sizeof(int*)); // Pointer for matrix rows
for(int i=0;i<A.rows;i++) mat[i] = malloc(A.cols*sizeof(int)); // Pointer of each matrix row to a matrix column
for (int i=0;i<A.rows;i++){
for (int j=0;j<A.cols;j++){
mat[i][j]=matrix[i][j];
}
}
A.matrix = mat;
return A;
}
int main() {
int mat[2][2] = {{1,2},{3,4}};
intMat A;
A = create_intMat(2,2,mat);
printf("%d\n",A.matrix[1][1]);
return 0;
}
I'm a beginner to pointers and got the pointer part of the code from another forum. I don't get it, though. int** mat is a pointer to a pointer to an int, so if I call it as is then it should give me back gibberish address, not the int being pointed to. However, the printf statement returns 4, which is the value of the int being pointed to! How come this is happening?

A.matrix is a pointer to a 2 pointers, which each point to 2 ints.
A.matrix[1] gets the second of those pointers - a pointer to 2 ints.
A.matrix[1][1] gets the second of those ints.

When you use:
int i;
int* p = &i;
or
int* p = malloc(sizeof(int));
p points to a single integer.
When you use:
int array[10];
int *p = array;
or
int *p = malloc(sizeof(int)*10);
p points to an array of 10 integers.
Similarly, a variable declared with:
int** mat;
can point to one int* or an array of int*s.
In your case, mat points to an array of 2 int*, each of which points to an array of 2 ints.
That makes use of mat[1][1] perfectly valid code.

Related

dereference a pointer to a pointer casted from array pointer

Learning C and this confusing me:
#include <stdio.h>
int main(){
int m[5][5];
int count = 0;
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
m[i][j] = count++;
}
}
int **p = m;
int (*k)[5] = m;
printf("%p\t%p\t%p", *p, *k, *m);
return 0;
}
that's what has been printed:
0x100000000 0x7ffff1fc9d70 0x7ffff1fc9d70
I'm really confused why dereference *p is 0x100000000, shouldn't it be 0x7ffff1fc9d70?
You’re thinking that m is of type int **, right?
It’s not. If you had done this, it would be:
int *m[5];
That make an array of five pointers to int, so m would be pointer to pointer to int.
However, if you do this:
int m[5][5];
You get enough space for 25 ints, which the compiler will access as a 5x5 two-dimensional array. p points to the beginning of that array. Dereference it, and the compiler reads the beginning of that array and interprets it as a pointer to int. Change the numbers you’re filling it with, and you’ll get different pseudo-pointers.

I get trash values when trying to print them and a segmentation fault [duplicate]

This question already has an answer here:
Dynamic memory access only works inside function
(1 answer)
Closed 4 years ago.
I wrote this simple code to test a bigger implementation I have and I get trash values plus a seg fault. One solution is to declare int* a and int* b as global and take out the arguments of fill. My question is, what is the consideration of memory handling in both cases, and why does it throw an error?
#include <stdio.h>
#include<stdlib.h>
#define LENGTH 4
void fill(int* a, int* b){
a = (int*) malloc(LENGTH * sizeof(int));
b = (int*) malloc(LENGTH * sizeof(int));
for(int i=0; i< LENGTH;i++){
a[i]=i;
b[i]=i+10;
}
}
void printArray(int* a, int* b){
for(int i = 0 ; i < LENGTH; i++)
printf("%d\n",a[i] );
for(int i = 0 ; i < LENGTH; i++)
printf("%d\n",b[i] );
}
int main(){
int* a;
int* b;
fill(a,b);
printArray(a,b);
}
You should pass pointers to pointers as arguments to the fill function so you can effectively modify the pointers a and b.
void fill(int** a, int** b){
*a = malloc(LENGTH * sizeof(int));
(*a)[i]=i;
fill(&a,&b);
Whenever you send args to a function in c they are always sent by value.
here, you send by value the addresses that a and b point to - they are uninitialized- so junk values.
When you use malloc- it returns a pointer to an address on the heap.
in your function the local values of a and b change. but in your main function they do not. if you want your function to change the address the pointers are pointing to in outside of the function you must send **a and **b. now when you change the address for a : * a= malloc() you change the value the pointer is holding. So now your a and b pointers will hold the new addresses allocated.
void fill(int** a, int** b)/*here you send by value the pointers addrsses */
{
int* aptr = NULL;
int* bptr = NULL;
*a = (int*) malloc(LENGTH * sizeof(int));/*update the value pointe a is pointing to */
*b = (int*) malloc(LENGTH * sizeof(int));
aptr = *a;
bptr = *b;
for(int i=0; i< LENGTH;i++){
aptr[i]=i;
bptr[i]=i+10;
}
}

what's *p when p is declared as int (*)[size] and assigned to an array

code:
int arr[5] = {1,2,3,4,5};
int (*p)[5] = &arr;
printf("p:%p\n",p);
printf("*p:%p\n",*p);
result: p = *p = arr = 0x7ffee517c830 they are all the address of the array
The right way to use p to visit arr[i] is *(*p+i)
The type of pointer p is int(*)[5], so p point to an array which type is int [5]. But we can't say that p point to an invisible shell of arr, p is a variable after all. It stores the address of arr, which is also the address of arr[0], the first element of arr.
I thought *p will get me 1, which is the first element of arr.
The dereference operation means take the value in p as address and get the value from this address. Right?
So p stores the address of arr,which is 0x7ffee517c830 here, and 1 is stored in this address. Isn't **p illegal? The first dereference give us 1, and second dereference will use 1 as address which is illegal.
What I am missing?
The result of *p is an lvalue expression of array type. Using (*p) is exactly the same as using arr in any expression you could now think of.
For example:
&*p means &arr
**p means *arr (which is legal).
(*p)[i] means arr[i].
sizeof *p means sizeof arr.
Arrays are not special in this regard. You can see the same phenomenon with int x; int *q = &x;. Now *q and x have exactly the same meaning.
Regarding your last paragraph, I think you are confusing yourself by imagining pointers as glorified integers. Some people teach pointers this way but IMO it is not a good teaching technique because it causes the exact confusing you are now having.
If you dereference an int(*)[5] you get an int[5] and that's all there is to it. The data type matters in dereferencing. It does not make sense to talk about "dereferencing 0x7ffee517c830". Again this is not peculiar to arrays; if you dereference a char ***, you get a char ** etc.
The only way in which arrays are "different" in this discussion is what happens if you try to do arithmetic on them, or output them, etc. If you supply an int[5] as a printf argument for example, there is implicit conversion to int * pointing at the first of those 5 ints. This conversion also happens when applying the * operator to an int[5], which is why you get an int out of that.
p is declared as a 'pointer to int[5]'.
arr is declared as an 'int[5]`
so the initializer p = &arr; is not really that strange. If you substituted any primitive type for int[5] you wouldn't bat an eye.
*p is another handle on arr. so (*p)[0] = 1.
This really only comes up in wierd cases. It's most natural where you dereference the pointer-to-array using the subscript operator. Here's a contrived example where I want to pass a table as argument.
#include <stdio.h>
int print_row_range(int (*tab) [2], int first, int last)
{
int i;
for(i=first; i<= last; i++)
{
printf("{%d, %d}\n", tab[i][0], tab[i][1]);
}
}
int main(int argc, char *argv[])
{
int arr[3][2] = {{1,2},{3,4},{5,6}};
print_row_range(arr,1,2);
}
This example treats the table as an array of rows.
Dereferencing doesn't give you a value. It gives you an object, which can be used as a value of its type if it can be converted to.
*p, being identical to arr, is an object of an array of 5 ints, so if you want to get an integer from the array, you must dereference it again like (*p)[3].
Consider a bigger example:
int arr[5][5];
int (*p)[5] = arr;
Now you get arr[0] with *p, which itself is an array of 5. Here comes the difference:
*( p+1) == arr[1];
*(*p+1) == arr[0][1];
^ ^^^
Got the point?
One use case is to be able to allocate with malloc an 2D (or more) pointer of arrays with only one malloc:
#include <stdio.h>
#include <stdlib.h>
static int (*foo(size_t n))[42] {
return malloc(sizeof *foo(0) * n);
// return malloc(sizeof(int [n][42]); works too
}
int main(void) {
size_t n = 42;
int (*p)[42] = foo(n);
if (!p) {
return 1;
}
printf("p:");
int accu = 0;
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < sizeof *p / sizeof **p; j++) {
p[i][j] = accu++;
printf(" %d", p[i][j]);
}
}
printf("\n");
free(p);
}
I think this very funny.
One more with VLA:
#include <stdio.h>
#include <stdlib.h>
static void *foo(size_t elem, size_t n, size_t m) {
return malloc(elem * n * m);
}
int main(void) {
size_t n = 42;
int (*p)[n] = foo(sizeof **p, n, n);
if (!p) {
return 1;
}
printf("p:");
int accu = 0;
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < sizeof *p / sizeof **p; j++) {
p[i][j] = accu++;
printf(" %d", p[i][j]);
}
}
printf("\n");
free(p);
}

How to access pointer to 2-d array in c struct?

I am having trouble with following struct for using it as Matrix
struct{
int col;
int row;
int (*p)[col];//In this line compiler is giving error, saying col undeclared here.
}Matrix;
I searched on internet and I found a solution which says to write
int (*p)[col]
as
int (*p)[]
Compiler passes it, no issues.
But when I want to increment p using Matrix variable say m
++(m.p);
compiler gives another errors (two) in the same line of code, which says
increment of pointer to unknown structure.
arithmetic on pointer to an incomplete type.
Please tell me why the compiler is giving the above mentioned errors?
What I finally want is to have a pointer in the structure which points to 2-d dynamic array of integers.
So,How to do it???
If you truly want a pointer to an arbitrary 2d array that changes, you will have to use a void pointer. (I don't recommend it, it's unsafe, design should probably be changed.)
struct
{
int col;
int row;
void* p;
}
Before you access the memory, use a local variable-length array pointer. Take the void pointer in the struct, and assign to it the
local vla pointer, using the information from the struct:
struct Matrix x = ...;
int (*n)[x.col] = x.p;
And then use it:
n[0][0] = ...
If you want to increment the void pointer in the struct, simply increment the local pointer, and assign it back to the void pointer:
n++;
x.p = n;
No casting is necessary, just a declaration of a local pointer. If that is a nuisance, operations on void pointer in the struct can be abstracted using inline functions. This should also be done for the sake of safety.
The field declaration int (*p)[col]; is not valid because the compiler don't know the value of col. What you need is a pointer to a pointer, int **p, where p[i] designates the i:th row in the two-dimensional array.
Here is a complete example with a convenient memory allocation macro:
#include <stdlib.h>
#define NEW_ARRAY(ptr, n) (ptr) = calloc((n) * sizeof (ptr)[0], sizeof (ptr)[0])
struct Matrix {
int rows;
int cols;
int **items;
};
void InitializeMatrix(int rows, int cols, struct Matrix *A)
{
int i;
A->rows = rows;
A->cols = cols;
NEW_ARRAY(A->items, rows);
for (i = 0; i < rows; i++) {
NEW_ARRAY(A->items[i], cols);
}
}
int main(void)
{
struct Matrix A;
InitializeMatrix(10, 20, &A);
return 0;
}
When an array is declared then the memory needs to be allocated separately.
struct Matrix{
int col;
int row;
int *Matrix[100];
};
More flexibile:
struct Matrix{
int col;
int row;
int **Matrix;
}
struct Matrix A;
A.col =10;
A.row = 10;
/* Allocate memory here for the Matrix */
You can declare and allocate memory for a 2D arrays using different methods.
/* Access the Matrix */
A.Matrix[i][j] = value;

What does this symbol ** mean in the C language

Hi I'm new to the C language, can someone explains what ** symbol mean.
typedef struct _TREENODE {
struct _TREENODE *Left, *Right;
TCHAR key[KEY_SIZE];
LPTSTR pData;
} TREENODE, *LPTNODE, **LPPTNODE;
If x is a pointer, *x dereferences it. **x is the same as *(*x), so **x dereferences a pointer to a pointer. (eg, it is the thing that is pointed to by the thing that x opints to).
** is a pointer to pointer, it is also used for dereferencing a pointer variable.
eg: int a=10,*b,**c;
b=&a;
c=&b;
printf("the a value is:%d\n",a);
printf("the b value is:%d\n",*b);
printf("the c value is:%d\n",**c);
just execute this code you will get the idea about pointer to pointer.
In you want to change a variable, you pass it by pointer (to the variable).
And if you want to change a pointer, you also pass it by pointer (to the pointer) which is a double pointer.
There are two things to know about * in C:
It's an operation. Doing *x on a pointer dereferences that pointer. Doing **x on a pointer can dereference a pointer to a pointer, and so on.
It's a type. Declaring a type of int *x means that it's a pointer to an int type. Declaring int **x means that it's a pointer to a pointer to an int type.
Example:
int main() {
int foo = 4;
int *bar = &foo; // declaring a pointer to int type *bar
int **baz = &bar; // declaring a pointer to a pointer to int type **baz
printf("foo: %d, *bar: %d, **baz: %d\n", foo, *bar, **baz); // derefencing the pointer *bar and **baz
return 0;
}
In a declaration, ** means pointer to a pointer. When evaluating an expression, ** dereferences a pointer to a pointer.
int** p; // Declares p to be a pointer to a pointer.
And...
**p = 10; // Dereferences p and assigns 10 to a memory location.
One common use of pointers to pointers is to represent dynamic 2D arrays. For example, if you want to create a matrix of M rows and N columns, you could do:
int** matrix = malloc(M*sizeof(*matrix));
int i = 0, j = 0;
for ( i = 0; i < M; ++i )
matrix[i] = malloc(N*sizeof(*matrix[0]));
Usage of the double pointer:
for ( i = 0; i < M; ++i )
for ( j = 0; j < N; ++j )
matrix[i][j] = 0; // Assigns a value to the element
// at the i-th row and j-th column.
If you want to use string pointer dereferencing, you would use:
for ( i = 0; i < M; ++i )
for ( j = 0; j < N; ++j )
*(*(matrix+i)+j) = 0;
Memory allocated for the matrix has to be freed in two passes also.
for ( i = 0; i < M; ++i )
free(matrix[i]);
free(matrix);
** means a pointer to a pointer.
Most of the time I like to think of it as "pointer(s)" to "a memory area". Which in fact may be a little redundant.
For example, suppose you have to dynamically store several words on memory, how would you do that? There are several ways to do this, but I'll provide an example that illustrate the use of **.
Now, suppose you want to store three words: hi, hello and goodbye
hi, hello and goodbye are strings, they consume, 2, 5 and 7 bytes on memory respectively. Well, in fact it's 3, 6 and 8 bytes because of the \0, but lets not get into many details.
But one thing is clear, we need three memory areas to hold these strings and also three pointers to reference these memory areas later.
Note that one can just declare three pointers that points to these memory areas, but, would you be willing to declare one thousand pointers to hold one thousand words? This is where ** kicks in.
Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUMBER_OF_WORDS 3
int
main(int argc, char **argv)
{
int i;
char **words;
/* pointers */
words = malloc(sizeof(char*)*NUMBER_OF_WORDS);
/* memory areas*/
words[0] = strdup("Hi");
words[1] = strdup("Hello");
words[2] = strdup("Goodbye");
for(i=0; i < NUMBER_OF_WORDS; i++)
printf("%d) %s\n", i, words[i]);
for(i=0; i < NUMBER_OF_WORDS; i++)
free(words[i]); /* memory area */
free(words); /* pointers */
return 0;
}

Resources