开发者

Comparison of a Char Pointer to NULL

I am running my code on SUSE Linux. I have a pointer which I make = NULL in a function. But the problem arises when I try to compare the same pointer with NULL in a while loop. This is leading to the program crashing. I have reproduced my problem in the sample code below. Can someone please tell me what is happening here?

My code is as below:

#include <stdio.h>

int func(char *,int);

int main()
{
    char buffer[20];
    int i =20;

    int temp = func(buffer,i);

    if ( (temp == 0) && (buffer != NULL) )
    {
        printf("inside loop \n");
    }
}

int func(char *ad,int a)
{
    ad = 开发者_如何学JAVANULL;
    printf("Integer is %d \n", a);
    return(0);
}

The problem is that the comparison, buffer != NULL is failing and the control goes inside the loop, which should not be happening ideally. I have resolved this, by doing this:

ad[0] = NULL and the comparison changed to buffer[0] != NULL.

Since NULL is used only in a pointer context, this is bad code. I could use '\0's instead of the NULL in my workaround and get away with not writing 'bad code', but I really want to know what is happening here. Can someone please clarify?

Thanks a ton, Aditya


buffer isn't a pointer, it cannot be NULL.

Your func sets the copied address of buffer to NULL. That does not affect buffer at all.

EDIT: Extended explanation

char buffer[20];

This reserves 20 bytes somewhere on stack. They're not initialized in any way. As you have 20 bytes of memory, obviously these bytes must reside at some address.

int temp = func(buffer,i);

This takes the address of your 20 bytes in buffer, and passes it to func.

int func( char *ad,int a)
{

ad = NULL;

Here you have, at a new position on stack, a new pointer variable which will exist only while func is executing. This pointer variable is at an address, and points to an address. You change this pointer, which affects where it points. But it will not change your original buffer in any way, since ad is only a temporary variable on stack that contained the address of the bytes in your buffer variable (until you set this temporary variable to NULL).


Two problems:

  1. In func, you're attempting to modify the value of ad, not what ad points to. This won't work, as changes to the parameter value are not reflected in the caller. You would need to change that code to

    
    int func(char **ad, int a)
    {
      *ad = NULL;
      printf("Integer is %d\n", a);
      return 0;
    }
    
    
    Unfortunately, this won't work with the rest of the code, since...

  2. buffer is declared as an array object, not a pointer, and you cannot modify an array object; IOW, buffer cannot be assigned to. Not to mention that the type of &buffer is char (*)[20], not char **.

Here's a modified version of your source that will "work", although I'm not sure what you're trying to accomplish:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 20

int func(char **ad, int a)
{
  *ad = NULL;                   // WOOPA! WOOPA! MEMORY LEAK!!! MEMORY LEAK!!!
  printf("Integer is %d\n", a);
  return 0;
}

int main(void)
{
  char *buffer = malloc(sizeof *buffer * SIZE);
  int i = 20;
  int temp = func(&buffer, i);
  if (temp == 0 && buffer != NULL)
    printf("Inside loop\n");
  return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜