Solved

References to pointers

Posted on 2004-08-05
5
235 Views
Last Modified: 2010-04-15
In C++ all references (by default) are initialized. Is it possible in C to have a reference to a pointer in which the pointer is null? Or does the pointer have to point to memory for you to take the reference of it?

e.g.

<type>* ptr1 = NULL;
<type>  ptr2;

ptr2 = &ptr1;

ipaman
0
Comment
Question by:ipaman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11725433
No problems with that. The variable ptr1 itself exists, so you can take it's address.
0
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 50 total points
ID: 11725450
Change your code:

<type>* ptr1 = NULL;
<type>**  ptr2; // note **

ptr2 = &ptr1;
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11725483
A pointer can point to anything or nothing or even a page-fault location. It is only when you dereference it that what it points to is accessed.

E.G.

char * a = (char *)1; // a points to an invalid memory location.
a += 1; // still invalid but no problem as I am not dereferencing.
char b [128]; // b is valid.
b[(int)a] = 'b'; // Valid as 'a' has the value 2;
a[(int)b] = '2'; // Also valid but you wouldnt want to do this.
a[0] = '0'; // Will probably page fault.

Paul
0
 

Author Comment

by:ipaman
ID: 11725487
bosman,

...but if you then try to dereference ptr2 (ptr2->...) you will wind up with an exception thrown,
correct?

0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11725640
Dereferencing ptr2 won't work, at least not the way you typed. Ptr2 is a pointer to a pointer to some type, so you'll need
    (*ptr2)->
to get something from the structure ptr1 points to. And ptr1 points to the location zero, usually not given to a user program as a writable location. The expression
    *ptr2
will work though, because that will get the content of ptr1 that contains NULL.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why adding test code changes the build ? 11 136
how to understand recursion 12 238
Detect CR LF to each line 12 185
upload values via post via proxy 1 19
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question