Unsual Result when passing pointer as argument
I have implemented a hashmap. struct hashmap_elmnts { char *key;
int in_use; void *data; //contains the address of the malloc of struct ussd defined below };
struct hashmap_map
{
int table_sizel;
int size;
struct hashmap_elmnts *element ;
};
Now, I have another structure USSD
typedef struct ussd { char menu;
int8_t did;
int8_t invokeid;
char *language;
char *msisdn;
char *ussd_string;
} USSD;
Now, while inserting in the HASHMAP, INITIALISATION :
USSD init = (USSD)malloc(sizeof(USSD)); init->msisdn = (char*)malloc(sizeof(char)); init->language = (char*)malloc(sizeof(char)); init->ussd_string = (char*)malloc(sizeof(char));
msisdn = language = ussd_string = NULL;
Then, I assign some values and then insert in the hashmap
However, while getting the information back, i face some issue:
IN FILE GET.c
I call a function
USSD *pointer = NULL;
res = hashget(key, pointer);
if (pointer == NULL)
printf ("pointer failn");
else
printf ("pointer passn");
IN FILE HASHGET.c
int hashget(int key, USSD_STRUCT *arg)
{
/** declaring a hashmap_map pointer **/
struct hashmap_map *m;
now, after various calculation -> i find a value of 'curr'
SO,
arg = (m->element[curr].data);
if (arg == NULL)
printf("failn");
else
printf ("passn"):
return 1;
}
THE OUTPUT IS AS FOLLOWS: pass pointer fail
How is this possible, when arg != NULL but still the pointer is NULL. As i expected any assignment at arg would be reflected in pointer also.
Can anybody let me what could have gone wrong. I'm unable to share the entire code snippets as the code is huge..
在你的第一个代码块中,你是在说if pointer equals null
if assign null to pointer
那么if assign null to pointer
if pointer equals null
。
if (pointer = NULL)
This is your problem. If your compiler isn't warning you about this, either turn your warnings up, or find a better compiler. What you want is what you have in your second if
block:
if (pointer == NULL)
The reason it isn't working with the one =
is because you are assigning NULL
to pointer
within the if statement. The result of that expression is NULL
, hence the if
block doesn't execute, but the else
block does.
Alternatively, some people find it easier to read by omitting the comparison to NULL
altogether:
if (pointer)
{
// pointer is not NULL
}
else
{
// pointer is NULL
}
Use this way (Make following changes at respective places)
...
res = hashget(key, &pointer);
if (pointer == NULL)
printf ("pointer failn");
else
printf ("pointer passn");
...
...
int hashget(int key, USSD_STRUCT *arg)
...
...
*arg = data;
if (*arg == NULL)
printf("failn");
else
printf ("passn"):
return 1;
}
...
Reason:
In your program you are passing pointer
as pass by value, In my method I am passing as pass by pointer or basically passing the address of pointer
because its value needs to be modified in calling function.
EDIT: (After your modification in the question)
USSD init = (USSD)malloc(sizeof(USSD));
This is wrong. init
should be a pointer.
Use USSD *init = (USSD*)malloc(sizeof(USSD));
.
And after you allocated memory to init->msisdn
, init->language
, init->ussd_string
, why are you setting them to NULL
, that too just by referencing the inner variables of struct
like this: msisdn = language = ussd_string = NULL
.
And even if you correct all these mistakes, my answer is OK, since you cannot modify value of pointer
by making a call like this: res = hashget(key, pointer);
, using this you can only modify *pointer
ie value pointed to by the pointer
.
下一篇: 将指针作为参数传递时的不确定结果