473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Re: C pointer question

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
2 2455
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
9
by: Cyron | last post by:
Hello friends, Recently I have begun exploring the features that the STL map collection provides. While learning how it worked, I encountered a result that confused me. Up until now, I had...
5
by: mdh | last post by:
Hi all, I have gone through the FAQ and done searches in the Comp.Lang and if I have missed it please let me have the ref. (Question generated in part by p119). Given char a;
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.