how to print value which pointer member pointing to in C - c

I have a struct
struct c
{
int *id;
int type;
} obj;
how to print what obj.id pointing to? and also point obj->id to some int variable
I tried
printf("%p\n",obj.id);
but above is printing some address
and
printf("%d\n",obj.id);
in above compiler gives warning
format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’

Since obj.id is a pointer to an int (an int*) you need to dereference it (using the * operator).
Full example:
#include <stdio.h>
struct c {
int *id;
int type;
} obj;
int main() {
int x = 10;
obj.id = &x;
printf("%d\n", *obj.id);
}

You need to dereference the pointer:
printf("%d\n",*(obj.id));

Related

Setting array of pointers

i'm doing a small exercise to load an array of pointers (double pointer) to a struct. I have the following definition in the header file:
#include <stdio.h>
#define LEN (5)
typedef struct sample_s {
int num;
char *name;
}sample_t;
typedef struct new_sample_s {
char *string;
sample_t **sample_arr;
}new_sample_t;
sample_t table[LEN] = {
{0, "eel"},
{1, "salmon"},
{2, "cod"},
{3, "tuna"},
{4, "catfish"}
};
and using the definitions int this .c file:
#include "test.h"
void print_new_sample_array(sample_t **sample_arr) {
int len = sizeof(table)/sizeof(new_sample_t);
for(int i = 0; i < len; i++){
printf("The array element is: %s\n", sample_arr[i]->name);
}
}
int main() {
new_sample_t new_sample;
new_sample.sample_arr = table;
print_new_sample_array(new_sample.sample_arr);
return 0;
}
I have two questions:
First I'm not sure how to correctly load the table to the new_sample.sample_arr
Error message here:
test.c: In function ‘main’:
test.c:13:27: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
new_sample.sample_arr = table;
^
Second, I'm not sure how I can refer to the properties of each element in the sample_arr. For example, when I do the following, the program errored out:
for(int i = 0; i < LEN; i++){
printf("This is the elem in the array: %s", new_sample[i]->name);
}
I'm trying to learn more about the double pointer concept and why I did it wrong. I would really appreciate the answer keeps the sample_arr as double pointer
Thank you!
In this assignment statement
new_sample.sample_arr = table;
the right operand (after implicit conversion of the array to pointer to its first element) has the type sample_t * while the left operand has the type sample_t ** due to the declaration of the data member
sample_t **sample_arr;
There is no implicit conversion from the type sample_t * to the type sample_t **. So the compiler issued a message.
You should declare the data member like
sample_t *sample_arr;
and correspondingly the function declaration will look like
void print_new_sample_array(sample_t *sample_arr);
And within the function the call of printf will look like
printf("The array element is: %s\n", sample_arr[i].name);

incompatible types when assigning to type struct * from struct

I can't understand why i get this error : The error is : "incompatible types when assigning to type 'PERSOANA * {aka struct *}' from type 'PERSOANA {aka struct }' "
Can you please explain me where is the mistake ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[20];
char name2[20];
char cnp[15];
} PERSON;
PERSON read_array(int n);
int main()
{
int n;
printf("n = ");
scanf("%d", &n);
PERSON *v;
v = read_array(n); //here i get the error
return 0;
}
PERSON read_array(int n) {
PERSON *v;
v = malloc(n * sizeof(PERSON));
for(int i = 0; i < n; i++) {
printf("name=");
gets(v[i].name);
//more instr
}
return v; // and also here
}
Return a pointer to PERSON, not the object PERSON.
// PERSON read_array(int n);
PERSON *read_array(int n);
// ^
// PERSON read_array(int n) {
// v
PERSON *read_array(int n) {
I can't understand why i get this error : Incompatible types when assigning to type PERSON from type PERSON.
I am reasonably confident that you do not get that error, but if you actually do then you should switch to a better compiler. I speculate that the error you get is instead
Incompatible types when assigning to type PERSON * from type PERSON
, because that's in fact what you are trying to do, given your declaration of function read_array().
From implementation and use, it appears that you want that function to return a pointer to a structure rather than a copy of the structure. That would be
PERSON *read_array(int n);
... and the same in the function definition.

pointer inside struct to dynamic 2-D array

Hope you can help me since I'm a little desperate right now. Im working with threads and I want them all to modify the same 2D array. So I made a struct with a pointer to the 2D array, but it isn't working. This is my code
typedef struct {
char XY;
int b;
int d;
int f;
int h;
} celdaMatriz;
typedef struct {
int totalX;
int totalY;
celdaMatriz **matriz;
char direction;
int i;
int j;
int cont;
} parameter;
As you can see parameter has the variable " celdaMatriz matriz**" (I'm sorry about the spanish in my code). Now comes a function to initialize a "celdaMatriz" 2D array
void createMatriz(int totalX, int totalY, celdaMatriz **matriz[totalX][totalY])
{
int i,j;
for(i=0;i<totalX; i++){
for(j=0; j<totalX;j++){
matriz[i][j]->XY=' ';
matriz[i][j]->b=0;
matriz[i][j]->d=0;
matriz[i][j]->f=0;
matriz[i][j]->h=0;
}
}
matriz[0][3]->XY='/';
matriz[2][0]->XY='*';
matriz[2][1]->XY='*';
matriz[0][2]->XY='*';
matriz[2][2]->XY='*';
matriz[1][3]->XY='*';
matriz[3][3]->XY='*';
}
the function createMatriz will change, but that's the idea. So now my main function
int main( int argc,char *argv[])
{
int i,j;
celdaMatriz **matriz = malloc( sizeof (*matriz) * 4);
for (i=0; i < 4; ++i)
matriz[i] = malloc(sizeof (**matriz) * 4);
crateMatriz(4,4,matriz);
parameter *p1,p2;
parameter *p1 = malloc(sizeof(*p1));
p1->totalX=4;
p1->totalY=4;
p1->i=0;
p1->j=0;
p1->cont=0;
p1->direction= 'f';
p1->matriz= matriz;
return 0;
}
So anytime I want to create a new thread I create a new parameter variable and its "matriz" variable is a pointer to the 2D array I created in the main function. Well at least that's the idea, but I get these errors
**request for member 'XY' in something not a structure or union**
**request for member 'b' in something not a structure or union**
**request for member 'd' in something not a structure or union**
**request for member 'f' in something not a structure or union**
**request for member 'h' in something not a structure or union**
passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by default]
main.c: 59:6: note: expected 'struct celdaMatriz ** (*) [(unsigned int) (totaly)]' but argument is of type 'struct celdaMatriz **
The errors come from the createMatrix function and the last one from the main function. By the way I'm using linux and C.
Thanks. Hope you can help me.
EDITED
I just fixed an error, this
*passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by default]
main.c: 59:6: note: expected 'struct celdaMatriz * () [(unsigned int) (totaly)]' but argument is of type 'struct celdaMatriz *
is already fixed. So the problem now is that I'm accessing matriz[i][j]->XY=' '; incorrectly
Hope you can help me since I'm a little desperate right now. Im working with threads and I want them all to modify the same 2D array. So I made a struct with a pointer to the 2D array, but it isn't working. This is my code
typedef struct {
char XY;
int b;
int d;
int f;
int h;
} celdaMatriz;
typedef struct {
int totalX;
int totalY;
celdaMatriz **matriz;
char direction;
int i;
int j;
int cont;
} parameter;
As you can see parameter has the variable " celdaMatriz matriz**" (I'm sorry about the spanish in my code). Now comes a function to initialize a "celdaMatriz" 2D array
void createMatriz(int totalX, int totalY, celdaMatriz **matriz[totalX][totalY])
{
int i,j;
for(i=0;i<totalX; i++){
for(j=0; j<totalX;j++){
matriz[i][j]->XY=' ';
matriz[i][j]->b=0;
matriz[i][j]->d=0;
matriz[i][j]->f=0;
matriz[i][j]->h=0;
}
}
matriz[0][3]->XY='/';
matriz[2][0]->XY='*';
matriz[2][1]->XY='*';
matriz[0][2]->XY='*';
matriz[2][2]->XY='*';
matriz[1][3]->XY='*';
matriz[3][3]->XY='*';
}
the function createMatriz will change, but that's the idea. So now my main function
int main( int argc,char *argv[])
{
int i,j;
celdaMatriz **matriz = malloc( sizeof (*matriz) * 4);
for (i=0; i < 4; ++i)
matriz[i] = malloc(sizeof (**matriz) * 4);
crateMatriz(4,4,matriz);
parameter *p1,p2;
parameter *p1 = malloc(sizeof(*p1));
p1->totalX=4;
p1->totalY=4;
p1->i=0;
p1->j=0;
p1->cont=0;
p1->direction= 'f';
p1->matriz= matriz;
return 0;
}
So anytime I want to create a new thread I create a new parameter variable and its "matriz" variable is a pointer to the 2D array I created in the main function. Well at least that's the idea, but I get these errors
**request for member 'XY' in something not a structure or union**
**request for member 'b' in something not a structure or union**
**request for member 'd' in something not a structure or union**
**request for member 'f' in something not a structure or union**
**request for member 'h' in something not a structure or union**
passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by default]
main.c: 59:6: note: expected 'struct celdaMatriz ** (*) [(unsigned int) (totaly)]' but argument is of type 'struct celdaMatriz **
The errors come from the createMatrix function and the last one from the main function. By the way I'm using linux and C.
Thanks. Hope you can help me.
EDIT
I just fixed an error, this
*passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by default]
main.c: 59:6: note: expected 'struct celdaMatriz * () [(unsigned int) (totaly)]' but argument is of type 'struct celdaMatriz *
is already fixed. So the problem now is that I'm accessing matriz[i][j]->XY=' '; incorrectly
EDIT
Hi. I just want to let you know that I already found an answer to my problem. Instead of send a pointer to the matrix, I will use it as a global variable, that way all the threads will be able to modify it. So the solution is 1- Declare a global celdaMatriz **matriz
celdaMatriz **matriz;
Next I changed the memory allocation
matriz = (celdaMatriz **) malloc(4 * sizeof(celdaMatriz *));
for (i = 0; i < 4; ++i)
{
matriz[i] = (celdaMatriz *) malloc(4 * sizeof(celdaMatriz));
}
Finally the new void createMatriz function:
void crearMatriz(int totalX, int totalY)
{
int i=0,j=0;
for(i=0; i<totalX;i++){
for(j=0; j<totalY; j++){
celdaMatriz *nodo = malloc(sizeof(celdaMatriz));
nodo->b=0;
nodo->d=0;
nodo->f=0;
nodo->h=0;
nodo->casilla=0;
matriz[i+j*totalY]=(celdaMatriz *)nodo;
}
}
matriz[12]->casilla=1;
for(i=0; i<totalX; i++){
for(j=0; j<totalY; j++){
printf(" %d ", matriz[i+j*totalY]->casilla);
}
printf("\n");
}
}
And that's it. Thanks to all of you who helped me with this problem
I guess that the problem is that you tried to call creatMatriz like this,
createMatriz(p1->totalX, p1->totalY, p1->matriz);
and C (C99) does not like it. The problem is that p1->matriz is of type struct celdaMatriz **, but your prototype of createMatriz asked for a struct * (*) [totalY] because C ignores [totalX].
If you want to keep the definition of parameter unchanged, I would suggest you just use a double pointer.

Function pointer confusion

I'm trying to use function pointers and Abstract data types in c. This is my first time using it and I'm really confused. Anyways when I tried to compile this code I gave me an error. The first time I ran it worked. But when I change the arguments by switch a and b. It gave me the old answer and never updated.
typedef struct data_{
void *data;
struct data_ *next;
}data;
typedef struct buckets_{
void *key;
}buckets;
typedef struct hash_table_ {
/* structure definition goes here */
int (*hash_func)(char *);
int (*comp_func)(void*, void*);
buckets **buckets_array;
} hash_table, *Phash_table;
main(){
Phash_table table_p;
char word[20]= "Hellooooo";
int a;
a = 5;
int b;
b = 10;
/*Line 11*/
table_p = new_hash(15, *print_test(word), *comp_func(&a, &b));
}
int *print_test(char *word){
printf("%s", word);
}
int *comp_func(int *i,int *j){
if(*i < *j){
printf("yeeeeeeee");
}else{
printf("yeaaaaaaaaaaaaa");
}
}
Phash_table new_hash(int size, int (*hash_func)(char *), int (*cmp_func)(void *, void *)){
int i;
Phash_table table_p;
buckets *buckets_p;
hash_table hash_table;
table_p = (void *)malloc(sizeof(hash_table));
/*Setting function pointers*/
table_p -> hash_func = hash_func;
table_p -> comp_func = cmp_func;
/*Create buckets array and set to null*/
table_p -> buckets_array = (buckets **)malloc(sizeof(buckets_p)*(size+1));
for(i = 0; i < size; i++){
table_p -> buckets_array[i] = NULL;
}
return table_p;
}
This is the error message:
functions.c: In function 'main':
functions.c:11:26: error: invalid type argument of unary '*' (have 'int')
functions.c:11:45: error: invalid type argument of unary '*' (have 'int')
Helloyeaaaaaaaaaaaaa
New error:
functions.c:11:3: warning: passing argument 2 of 'new_hash' makes pointer from integer without a cast [enabled by default]
hash.h:29:13: note: expected 'int (*)(char *)' but argument is of type 'int'
functions.c:11:3: warning: passing argument 3 of 'new_hash' makes pointer from integer without a cast [enabled by default]
hash.h:29:13: note: expected 'int (*)(void *, void *)' but argument is of type 'int'
If you want to pass a function as a function-pointer, simply provide the name:
new_hash(15, print_test, comp_func);
or alternatively (and equivalently), use the & symbol to make it clear what's going on:
new_hash(15, &print_test, &comp_func);
You should declare function before using it. If you don't do this, the compiler assumes that it returns int, and gives you an error when you try to dereference it (since it's impossibole to dereference int).
EDIT:
you may also misunderstood the concept of function pointers. you should not pass the result of print_test(word) to new_hash - you should pass print_test itself. (also, change its return type)

warning: initialisation from incompatible pointer type

Hi all it's been sometime since I've touched C so I'm really rusty on it. I wrote a small program to create a matrix using two dynamic arrays. However, I'm getting this warning and I don't understand why? I guess I'm not quite sure about pointers to pointers. Can someone help me point out where my problem is? Thanks.
sm.c: In function ‘main’:
sm.c:11:13: warning: initialisation from incompatible pointer type [enabled by default]
sm.c: In function ‘makeMatrix’:
sm.c:27:3: warning: return from incompatible pointer type [enabled by default]
#include <stdio.h>
#include <stdlib.h>
typedef int (**intptr) ();
intptr makeMatrix(int n);
int main(int argc, char *argv[]) {
int n = 2;
int **A = makeMatrix(n);
if(A) {
printf("A\n");
}
else printf("ERROR");
}
intptr makeMatrix(int size) {
int **a = malloc(sizeof *a * size);
if (a)
{
for (int i = 0; i < size; i++)
{
a[i] = malloc(sizeof *a[i] * size);
}
}
return a;
}
You've got some problems here:
typedef int (**intptr) ();
intptr makeMatrix(int n);
...
int **A = makeMatrix(n);
The intptr typedef declares a pointer to a pointer to a function that takes an indeterminate number of arguments and returns an int. A is not an int.
You need to write:
int **makeMatrix(int n);
int **A = makeMatrix(n);
Using a typedef won't help much here.
typedef int **(*intptr)();
That declares a pointer to a function that returns a pointer to a pointer to an int. But writing
intptr makeMatrix(int n);
would declare that makeMatrix() returns a pointer to a function, not an int **.
Your typedef has an extra (), making it a zero argument function type. Remove that and you should be good.

Resources