473,839 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2501
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.invalidwrot e:
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********@gma il.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
5039
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
20
6582
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 area of ram that's not reserved by the program. Accessing memory through such pointer is likely to result in core dump (memory access violation)"
4
3638
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 useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
204
13153
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 = {0,1,2,4,9};
17
2730
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 compiler I use for the Samsung 16 bit Smartcard chips and I want to know if it is compliant with the ANSI standard or if it violates it. Have a look at this super simple example, where the value of b is incorrect:
26
3075
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 read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
9
3212
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 always been under the impression that if I had a pointer variable, p which contained the member "first", I could access this member variable either by: (*p).first OR p->first . That is, in general I thought p->x was just shorthand for (*p).x ?
5
1666
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
3379
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 array of int */\ ap = &a1; printf("%d\n", **ap);
0
9856
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10597
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10656
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10299
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7834
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.