473,385 Members | 1,661 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,385 software developers and data experts.

Pointer to pointer

hi friends, don't understand this:

/*
**p is a pointer to *a[], but if *a stops increasing when reads abc,
where is pointing *p??
*/

#include <stdio.h>

int main(void)
{
char *a[] = {"abc", "def", "ghi", "jkl", NULL};
char **p;

p = (char **)a;
while (*p) {
while (**p) {
printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
(*p)++;
}
p++;
}

return 0;
}

/*
Returns
david@debian:~$ ./demo
*a=0x4005ec *p=0x4005ec a abc
*a=0x4005ed *p=0x4005ed b bc
*a=0x4005ee *p=0x4005ee c c
*a=0x4005ef *p=0x4005f0 d def -----Here *a stops inc, ¿where is
pointing *p?
*a=0x4005ef *p=0x4005f1 e ef
*a=0x4005ef *p=0x4005f2 f f
*a=0x4005ef *p=0x4005f4 g ghi
*a=0x4005ef *p=0x4005f5 h hi
*a=0x4005ef *p=0x4005f6 i i
*a=0x4005ef *p=0x4005f8 j jkl
*a=0x4005ef *p=0x4005f9 k kl
*a=0x4005ef *p=0x4005fa l l
*/

Nov 21 '08 #1
5 2188
david wrote:
hi friends, don't understand this:

/*
**p is a pointer to *a[], but if *a stops increasing when reads abc,
where is pointing *p??
*/

#include <stdio.h>

int main(void)
{
char *a[] = {"abc", "def", "ghi", "jkl", NULL};
char **p;

p = (char **)a;
In most contexts, an array name is automatically converted into a
pointer to the first element of the array, so the simple expression 'a'
already has the type "char**", there's no need for the cast. If a cast
were necessary, it would also indicate a design error.
while (*p) {
while (**p) {
printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
The "%p" format specifier expects a void* argument. This is one of the
few places where a cast is routinely needed and perfectly correct:

printf("*a=%p *p=%p %c %s\n", (void*)*a, (void*)*p, **p, *p);
(*p)++;
}
p++;
}

return 0;
}

/*
Returns
david@debian:~$ ./demo
*a=0x4005ec *p=0x4005ec a abc
*a=0x4005ed *p=0x4005ed b bc
*a=0x4005ee *p=0x4005ee c c
Up to this point, p == a, so *p == *a, or in other words, *p == a[0].
Then you execute p++. The result is that p == a+1, so *p == *(a+1), or
in other words *p == a[1].
*a=0x4005ef *p=0x4005f0 d def -----Here *a stops inc, ¿where is
pointing *p?
Since p is now pointing at a[1], (*p)++ no longer changes the value of
*a. It is now changing the value of *(a+1).
Nov 21 '08 #2
david <da*******@gmail.comwrites:
hi friends, don't understand this:

/*
**p is a pointer to *a[], but if *a stops increasing when reads abc,
where is pointing *p??
*/

#include <stdio.h>

int main(void)
{
char *a[] = {"abc", "def", "ghi", "jkl", NULL};
char **p;

p = (char **)a;
I'd drop the cast.
while (*p) {
while (**p) {
printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
Technical matter: %p expects arguments of type void * and the
conversion is not done automatically so you need to write:

printf("*a=%p *p=%p %c %s\n", (void *)*a, (void *)*p, **p, *p);

Initially, *a and *p refer t the same object (a[0] -- the first
pointer in a). when you...
(*p)++;
increment *p, you also change *a. But as soon as you...
}
p++;
move p on to the next element (a[1]), *a will remain the same.
}

return 0;
}

/*
Returns
david@debian:~$ ./demo
*a=0x4005ec *p=0x4005ec a abc
*a=0x4005ed *p=0x4005ed b bc
*a=0x4005ee *p=0x4005ee c c
*a=0x4005ef *p=0x4005f0 d def -----Here *a stops inc, ¿where is
pointing *p?
*a=0x4005ef *p=0x4005f1 e ef
*a=0x4005ef *p=0x4005f2 f f
*a=0x4005ef *p=0x4005f4 g ghi
*a=0x4005ef *p=0x4005f5 h hi
*a=0x4005ef *p=0x4005f6 i i
*a=0x4005ef *p=0x4005f8 j jkl
*a=0x4005ef *p=0x4005f9 k kl
*a=0x4005ef *p=0x4005fa l l
*/
--
Ben.
Nov 21 '08 #3
Thanks James, and thanks for the corrections too!!!
Nov 21 '08 #4
Thanks ben!!!
Nov 21 '08 #5
david wrote:
hi friends, don't understand this:

/*
**p is a pointer to *a[], but if *a stops increasing when reads abc,
where is pointing *p??
*/

#include <stdio.h>

int main(void)
{
char *a[] =3D {"abc", "def", "ghi", "jkl", NULL};
char **p;

p =3D (char **)a;
while (*p) {
while (**p) {
printf("*a=3D%p *p=3D%p %c %s\n", *a, *p, **p, *p);
(*p)++;
}
p++;
}

return 0;
}

/*
Returns
david@debian:~$ ./demo
*a=3D0x4005ec *p=3D0x4005ec a abc
*a=3D0x4005ed *p=3D0x4005ed b bc
*a=3D0x4005ee *p=3D0x4005ee c c
*a=3D0x4005ef *p=3D0x4005f0 d def -----Here *a stops inc, =BFwhere is
pointing *p?
[...]
*/
Here's the state of p and the elements of a. You're incrementing the
actual pointers in the array, then once they reach the nul terminator,
moving to the next pointer in the array. (view in a monospace font, like
Courier)

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^

*a=3D0x4005ec *p=3D0x4005ec a abc

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^

*a=3D0x4005ed *p=3D0x4005ed b bc

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^

*a=3D0x4005ee *p=3D0x4005ee c c

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^

*a=3D0x4005ef *p=3D0x4005f0 d def

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^

*a=3D0x4005ef *p=3D0x4005f1 e ef

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^

*a=3D0x4005ef *p=3D0x4005f2 f f

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^

*a=3D0x4005ef *p=3D0x4005f4 g ghi

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^

*a=3D0x4005ef *p=3D0x4005f5 h hi

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^

*a=3D0x4005ef *p=3D0x4005f6 i i

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^

*a=3D0x4005ef *p=3D0x4005f8 j jkl

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^

*a=3D0x4005ef *p=3D0x4005f9 k kl

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^

*a=3D0x4005ef *p=3D0x4005fa l l

"abc\0" "def\0" "ghi\0" "jkl\0"
a[0]^ a[1]^ a[2]^ a[3]^ a[4]=NULL
p^
Nov 21 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
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 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
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...

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.