2

I have tried many tutorials and got more confused. So please teach this noob with simplest answers. If possible then just stick to the question......i am having information overload :)

first the main function

main(){
  int y=0;
  display(&y);
}

Now the function

int display(int* x){
  //do something depending on different values of x
}

One of the case is following: "if no variable is pointed to, that is, if the address of the variable is NULL, your function sets the value of the variable pointed to zero."

Now my understanding in display function i need to do

if (x==NULL)
   *x=0;

Now here is where I am getting stuck with...... - if i call the function with display(NULL); I get following error in Visual "Unhandled exception at 0x00C84036 in BTP 300 A1.exe: 0xC0000005: Access violation writing location 0x00000000."

How do i store some value in y from a function if its address is null?

or the question is just wrong and it should have said value stored at the address, to which pointer variable points, is null i.e y=Null ???

If int* z is a null pointer than what is the value of the address to which z points to and what is the value that is stored in address that is pointed???

5
  • 6
    The requirement seems wrong: if no variable is pointed to, there is no variable whose value you can set to zero via the pointer. Commented Sep 29, 2013 at 9:35
  • Simply put - you can't. The fact that It's a NULL pointer indicates that it points to no valid memory. If you passed the address of the pointer, you could examine the location pointed to. If it proved to be NULL, you could (a) allocate some memory (b) using your knowledge of the ptr's address, you could set the pointer to point to this new memory (c) copy some meaningful data into this new mem. Commented Sep 29, 2013 at 9:37
  • 1
    It's just garbage. Clearly you understand more than your teacher. Commented Sep 29, 2013 at 9:38
  • Oh look, a bug in the technical specifications written by someone other than a programmer. How unexpected. Commented Sep 29, 2013 at 9:42
  • 4
    It's possible that there are other requirements in the assignment that change the context to something that makes sense. For example, is the prototype of display a given? What does the return value mean? Maybe they mean display should return 0 if NULL is passed in? Either way, it's something that needs to be clarified by the teacher, not by SO. If they don't, press harder. There's lots of crummy teachers around. Commented Sep 29, 2013 at 10:26

2 Answers 2

2

Most likely, this means:

int value_to_display = x? *x: 0;
// do something with value_to_display

i.e., the requirement is to treat a null pointer the same as a pointer to zero.

Sign up to request clarification or add additional context in comments.

Comments

0

These two lines won't work :

if (x==NULL)
*x=0;

as you are trying to de-reference NULL which is forbidden.

What you want to do is something like :

if (x==NULL)
    x = new int(0);

4 Comments

Please do not do that (unless you ensure the new pointer is managed well)!
But that's meaningless within display function, as it would modify x passed by value, so that the original variable y won't be changed.
@IgorR. One could argue that this entire assignment seems a bit meaningless.
Why would you want to do that? I cannot see anything that would justify making a dynamic allocation here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.