Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Basic question about pointers

Reply
Thread Tools

Basic question about pointers

 
 
Urs Beeli
Guest
Posts: n/a
 
      07-17-2007
On Mon, 16 Jul 2007 10:42:13 -0000 Mohan wrote:

[Quoting Mohan as my server no longer carries the OP]

> Aarti wrote:
> > I have a very elementary question about pointers. Please pardon me
> > for my ignorance of C
> >
> > int main()
> > {
> > int* i;
> > *i = 1 //at times this may give me a core dump.
> > const char* str = "test"; //This seems to be a valid construct.
> > }
> >
> > Now I understand that when we declare int* i, we just have a pointer
> > that may point to any place. If we try to store something in it, we
> > may get a segmentation fault. But why does const char* str = "test"
> > work?


I think you are confusing things due to different notations.

You have:
int* i;
*i = 1;

This is not correct, as i is not properly initialised so dereferencing it on
the second line causes undefined behaviour.

What you have above, is *NOT* the same as:
int* i = 1;
Here you create a pointer to an int and make it point to the address "1",
not defining the content of *i.

Had you written:
char* str;
*str = "test";
you would have had the same problem (plus an additional one), you would
have dereferenced the uninitialized pointer to char str. (And even had it
been initialised, it would be a syntax error to assign as string literal to
it).

Writing:
char* str;
str = "test";
would be correct and do the same thing as your example, but then
int* i;
i = 1;
would also be valid (and do the same as int *i=1 but not do what you
tried.

The fact that the legal construct 'int *i=1;' contains the sequence '*i=1;'
does not mean that i is dereferenced, the * belongs to the declaration of i.

I imagine that this might have confused you.

Cheers
/urs

--
"Change is inevitable, except from a vending machine."
-- Urs Beeli, <usenet@CONCAT_MY_FIRST_AND_LAST_NAME.ch>
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      07-17-2007
Urs Beeli wrote:
> I think you are confusing things due to different notations.
>
> You have:
> int* i;
> *i = 1;
>
> This is not correct, as i is not properly initialised so dereferencing it on
> the second line causes undefined behaviour.
>
> What you have above, is *NOT* the same as:
> int* i = 1;
> Here you create a pointer to an int and make it point to the address "1",
> not defining the content of *i.


Here you have a constraint violation. `1` is not a legal value for a
pointer. (If you cast the integer to a pointer, only the implementation
knows what will happen. I think it has to tell you, though.)

--
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

 
Reply With Quote
 
 
 
 
Urs Beeli
Guest
Posts: n/a
 
      07-17-2007
On Tue, 17 Jul 2007 13:35:38 +0100 Chris Dollin wrote:
> Urs Beeli wrote:
> > I think you are confusing things due to different notations.
> >
> > You have:
> > int* i;
> > *i = 1;
> >
> > This is not correct, as i is not properly initialised so dereferencing it on
> > the second line causes undefined behaviour.
> >
> > What you have above, is *NOT* the same as:
> > int* i = 1;
> > Here you create a pointer to an int and make it point to the address "1",
> > not defining the content of *i.

>
> Here you have a constraint violation. `1` is not a legal value for a
> pointer. (If you cast the integer to a pointer, only the implementation
> knows what will happen. I think it has to tell you, though.)


Fair enough, I missed that in my zeal to unconfuse the OP

Cheers
/urs

--
"Change is inevitable, except from a vending machine."
-- Urs Beeli, <usenet@CONCAT_MY_FIRST_AND_LAST_NAME.ch>
 
Reply With Quote
 
Bryan Harris
Guest
Posts: n/a
 
      07-17-2007
On Jul 15, 10:14 pm, Aarti <(E-Mail Removed)> wrote:
> int main()
> {
> int* i;
> *i = 1 //at times this may give me a core dump.
> const char* str = "test"; //This seems to be a valid construct.
>
> }


Shouldn't there be a semicolon on line 4?

1: int main()
2: {
3: int* i;
4: *i = 1; //at times this may give me a core dump.
5: const char* str = "test"; //This seems to be a valid construct.
6:
7: }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Basic interview question on pointers pauldepstein@att.net C++ 6 03-04-2009 10:04 AM
pointers and assignment operators, basic question Jeff Bender C++ 6 06-08-2007 10:26 PM
Basic pointers question Simon C Programming 6 11-17-2006 03:23 PM
pointers to pointers question Chad C Programming 5 03-27-2006 07:44 AM



Advertisments