By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,677 Members | 1,298 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,677 IT Pros & Developers. It's quick & easy.

Re: C pointer question

P: n/a
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <ke******@ceid.upatras.grwrote:
See for example what this short program will print:
[...]
static void
showtext(const char **ptr)
{
if (ptr == NULL)
printf("invalid argument\n");
if (*ptr == NULL)
printf("a null pointer\n");
else
printf("a pointer to the text `%s'\n", *ptr);
}
My apologies. I should have been less hasty to hit `post'. If showtext()
is passed a null pointer, it may attempt to dereference the null pointer.
A better definition of the function would be:

static void
showtext(const char **ptr)
{
if (ptr == NULL) {
printf("invalid argument\n");
return;
}

if (*ptr == NULL)
printf("a null pointer\n");
else
printf("a pointer to the text `%s'\n", *ptr);
}

Oct 5 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Giorgos Keramidas wrote:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <ke******@ceid.upatras.grwrote:
>See for example what this short program will print:
[...]
static void
showtext(const char **ptr)
{
if (ptr == NULL)
printf("invalid argument\n");
if (*ptr == NULL)
printf("a null pointer\n");
else
printf("a pointer to the text `%s'\n", *ptr);
}

My apologies. I should have been less hasty to hit `post'. If showtext()
is passed a null pointer, it may attempt to dereference the null pointer.
A better definition of the function would be:

static void
showtext(const char **ptr)
{
if (ptr == NULL) {
printf("invalid argument\n");
return;
}

if (*ptr == NULL)
printf("a null pointer\n");
else
printf("a pointer to the text `%s'\n", *ptr);
}
Alternatively, you could just have added `else' right
before the second `if'.

Unfortunately, I see no other messages on this thread
despite the "Re:" in the subject. Would you mind repeating
whatever question you had, for those of us who have, er,
lost the thread?

--
Eric Sosman
es*****@ieee-dot-org.invalid
Oct 5 '08 #2

P: n/a
On Sun, 05 Oct 2008 17:42:15 -0400, Eric Sosman <es*****@ieee-dot-org.invalidwrote:
Giorgos Keramidas wrote:
>My apologies. I should have been less hasty to hit `post'. If
showtext() is passed a null pointer, it may attempt to dereference
the null pointer. A better definition of the function would be:

static void
showtext(const char **ptr)
{
if (ptr == NULL) {
printf("invalid argument\n");
return;
}

if (*ptr == NULL)
printf("a null pointer\n");
else
printf("a pointer to the text `%s'\n", *ptr);
}

Alternatively, you could just have added `else' right before the
second `if'.
Indeed :)
Unfortunately, I see no other messages on this thread despite the
"Re:" in the subject. Would you mind repeating whatever question you
had, for those of us who have, er, lost the thread?
It was a reply to a comp.std.c article. I thought I had cross-posted my
first, but I somehow managed to mess things up. Here's the original
reply, including all the relevant bits of the original post:

: Date: Sun, 05 Oct 2008 18:22:13 +0300
: From: Giorgos Keramidas <ke******@ceid.upatras.gr>
: Followup-To: comp.lang.c
: Subject: Re: C pointer question
: Newsgroups: comp.std.c
: Message-ID: <87************@kobe.laptop>
:
: On Sun, 5 Oct 2008 03:35:23 -0700 (PDT), apple_amateur <an********@gmail.comwrote:
: Hi all,
: >
: I found the following function, which is used to remove the head of a
: linked list.
:
: Hi,
:
: This is a general "C" question.
:
: Please do not post general C questions to `comp.std.c'. This group is
: meant to be a discussion board for topics related to the C _standard_
: itself, not the language or its use.
:
: I have set the `Followup-To:' header to `comp.lang.c'. This is probably
: a more appropriate place for this sort of question. Please keep replies
: to that group.
:
: void RemoveHead(node **head)
: {
: node *temp;
: if (head && *head) { /* Corrected code */
: temp = (*head)->next;
: free(*head);
: *head = temp;
: }
: }
: >
: 1. Why is the purpose of 'head' in the 'if' condition? Shouldn't
: '*head' suffice?
:
: No it wouldn't be sufficient as a check. You cannot `read' or otherwise
: access the value of `*head', not even to compare it to a null pointer,
: until you have checked that `head' itself is a non-null pointer. Hence
: the need for both checks.
:
: 2. This function has an argument, which is a pointer to a pointer.
: Presumably, 'head' is a pointer to '*head', which is another pointer
: to the address of the link list head. If 'head' does not point to
: NULL, does it follow that '*head' does not point to NULL? If so, why?
:
: No it doesn't. See for example what this short program will print:
:
: #include <assert.h>
: #include <stdio.h>
: #include <stdlib.h>
:
: static void showtext(const char **ptr);
:
: int
: main(void)
: {
: const char *text = NULL;
: const char **ptr = &text;
:
: assert(ptr != NULL);
: showtext(ptr);
:
: text = "Hello world";
: showtext(ptr);
:
: return EXIT_SUCCESS;
: }
:
: static void
: showtext(const char **ptr)
: {
: if (ptr == NULL)
: printf("invalid argument\n");
: if (*ptr == NULL)
: printf("a null pointer\n");
: else
: printf("a pointer to the text `%s'\n", *ptr);
: }
:
: The first time showtext() is called, the `ptr' pointer points to the
: automatic storage of the `text' pointer and the `text' pointer is a null
: pointer, so the output text is:
:
: a null pointer
:
: The second time showtext() is called, the `ptr' pointer points to the
: _same_ automatic storage area. But the `text' pointer in that area now
: is not a null pointer anymore. It points to the string "Hello world".
: So the output text is:
:
: a pointer to the text `Hello world'
:
: The full output of the program should be something like:
:
: $ ./a.out
: a null pointer
: a pointer to the text `Hello world'
: $
:
: See how the `ptr' pointer only needs to be initialized _once_ and it
: works both times? Try to step through this short example, and try to
: understand where each one of the two pointers is stored, what is the
: value of each pointer, and _where_ it points.
:
: It will hopefully clear things up a bit :)

Oct 5 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.