For Programmers: Free Programming Magazines  


Home > Archive > C > February 2006 > Re: Help in c pointers









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Help in c pointers
Zero

2006-02-28, 7:55 am


manochavishal@gmail.com schrieb:

> int *p;
> *p = 4;


You have done the greates programming fault when using pointers.
Never use a pointer when it points somewhere!

Better try this:

int * p;

p = (int *) malloc (sizeof(int));

*p = 4;

A. Sinan Unur

2006-02-28, 7:55 am

"Zero" <chefmuetze@web.de> wrote in news:1141121017.828706.126940
@e56g2000cwe.googlegroups.com:

> manochavishal@gmail.com schrieb:
>
>
> You have done the greates programming fault when using pointers.
> Never use a pointer when it points somewhere!


ITYM: Never use a pointer if it does *not* point to anywhere (that is,
never use an uninitialized pointer.

> Better try this:
>
> int * p;
>
> p = (int *) malloc (sizeof(int));
>
> *p = 4;


Better not. There is no need to cast the return value of malloc, and
doing so can hide errors. Always, yes *always*, check that malloc
succeeded:

int *p = malloc(sizeof *p);
if ( p ) {
*p = 4;
} else {
/* handle error */
}

Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html

Keith Thompson

2006-02-28, 7:55 am

"manochavishal@gmail.com" <manochavishal@gmail.com> writes:
> But again why i am able to pass value to char* even if it is not
> intialized.


Please read <http://cfaj.freeshell.org/google/>.

If I understand your question, it's because you're invoking undefined
behavior, which can do anything. See the response I posted a few
minutes ago on the "about the array" thread.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson

2006-02-28, 7:55 am

"A.A" <auto.ptr@gmail.com> writes:
> Because you 're luck.


Ok, *everybody* who's reading this through Google, run, do not walk,
to <http://cfaj.freeshell.org/google/>. Read it. Understand it.
Do this before you post here again. Please.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Herbert Rosenau

2006-02-28, 6:56 pm

On Tue, 28 Feb 2006 10:57:43 UTC, "manochavishal@gmail.com"
<manochavishal@gmail.com> wrote:

> Hi keith,
>
> Can u recommend me soem of the advance C books.


I don't know who u is. Why does you ask us if u knows it? Ask
she/he/it
yourself! Maybe u will answer you, maybe not. I've never seen a
message from Mr./Ms./Mrs. u in usenet at all.

I have gone thru Expert
> C programming. Some book on similiar line sbut with multi threading and
> algorithms.


Mr. google is your friend on that question.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com