And I thought I understood it, finally. Alas.
given:
char *s[]={"Jan","Feb","Mar","April"};
is it possible to have char *p point at s?
*p = s...does not do it, as I expect.
*p=s[0]...I believe sets it to point to the first element in s. But
this is not what I want. What I want is a pointer which, when
incremented will produce "Jan","Feb","Mar","April" etc.
I believe what I want is a pointer equivalent of:
printf("%s", s[i]), (where i = 0->3)
Thanks. 14 71107
mdh wrote:
And I thought I understood it, finally. Alas.
given:
char *s[]={"Jan","Feb","Mar","April"};
is it possible to have char *p point at s?
It might be conceptually easier to think of s as a char**.
so given char* p;
p = s[0];
p now points to the string literal "Jan".
*p = s...does not do it, as I expect.
*p=s[0]...I believe sets it to point to the first element in s. But
this is not what I want. What I want is a pointer which, when
incremented will produce "Jan","Feb","Mar","April" etc.
Then you want p to be another char**.
I believe what I want is a pointer equivalent of:
printf("%s", s[i]), (where i = 0->3)
char** p = s;
for( unsigned n = 0; n < 4; ++n )
{
printf("%s", *p );
++p;
}
--
Ian Collins.
On Apr 17, 6:59 pm, Ian Collins <ian-n...@hotmail.comwrote:
mdh wrote:
given:
char *s[]={"Jan","Feb","Mar","April"};
>
It might be conceptually easier to think of s as a char**.
Then you want p to be another char**.
char** p = s;
for( unsigned n = 0; n < 4; ++n )
{
printf("%s", *p );
++p;
Ian...got "JanJanJan"
if I replaced *p with *p++ got as expected.
Does this make sense?
mdh <m...@comcast.netwrote:
And I thought I understood it, finally. Alas.
given:
char *s[]={"Jan","Feb","Mar","April"};
is it possible to have char *p point at s?
No.
*p = s...does not do it, as I expect.
*p=s[0]...I believe sets it to point to the first element in s. But
this is not what I want. What I want is a pointer which, when
incremented will produce "Jan","Feb","Mar","April" etc.
To point to an element, you need a pointer to the element type.
To point to an element of an array of elements, you need a
pointer to the element type.
So, to point to an element of an array of X, you need a pointer
to X.
Thus, to point to an element of an array of char *, you need
a pointer to char *. In other words, you need a char **.
I believe what I want is a pointer equivalent of:
printf("%s", s[i]), (where i = 0->3)
#include <stdio.h>
#define countof(X) ( (size_t) ( sizeof(X)/sizeof*(X) ) )
int main(void)
{
const char *s[]={"Jan","Feb","Mar","April"};
const char **p;
size_t i;
puts("Loop 1:");
for (i = 0; i < countof(s); i++)
puts(s[i]);
puts("\nLoop 2:");
for (p = s, i = 0; i < countof(s); i++)
puts(p[i]);
puts("\nLoop 3:");
for (p = s; p < &s[countof(s)]; p++)
puts(*p);
return 0;
}
mdh wrote:
>
>>It might be conceptually easier to think of s as a char**. Then you want p to be another char**.
>char** p = s;
for( unsigned n = 0; n < 4; ++n ) { printf("%s", *p ); ++p;
Ian...got "JanJanJan"
if I replaced *p with *p++ got as expected.
Does this make sense?
I didn't test what I posted, but I just tried this and it worked as
expected. There shouldn't be any difference.
#include <stdio.h>
int main(void)
{
char *s[]={"Jan","Feb","Mar","April"};
char** p = s;
for( unsigned n = 0; n < 4; ++n )
{
printf("%s\n", *p );
++p;
}
}
--
Ian Collins.
On Apr 17, 7:11 pm, Peter Nilsson <a...@acay.com.auwrote:
mdh <m...@comcast.netwrote:
given:
char *s[]={"Jan","Feb","Mar","April"};
is it possible to have char *p point at s?
No.
ok.....
To point to an element, you need a pointer to the element type.
To point to an element of an array of elements, you need a
pointer to the element type.
So, to point to an element of an array of X, you need a pointer
to X.
Thus, to point to an element of an array of char *, you need
a pointer to char *. In other words, you need a char **.
Thank you Ian...that's a nice explanation.
>
#include <stdio.h>
#define countof(X) ( (size_t) ( sizeof(X)/sizeof*(X) ) )
int main(void)
{
const char *s[]={"Jan","Feb","Mar","April"};
const char **p;
size_t i;
puts("Loop 1:");
for (i = 0; i < countof(s); i++)
puts(s[i]);
puts("\nLoop 2:");
for (p = s, i = 0; i < countof(s); i++)
puts(p[i]);
puts("\nLoop 3:");
for (p = s; p < &s[countof(s)]; p++)
puts(*p);
return 0;
}
Thank you....and thank you for the code..I will follow along in the
debugger.
Michael.
On Apr 17, 7:11 pm, Peter Nilsson <a...@acay.com.auwrote:
Thus, to point to an element of an array of char *, you need
a pointer to char *. In other words, you need a char **.
Is this thus the equivalent of *argv[] which we see in main?
mdh said:
On Apr 17, 7:11 pm, Peter Nilsson <a...@acay.com.auwrote:
>Thus, to point to an element of an array of char *, you need a pointer to char *. In other words, you need a char **.
Is this thus the equivalent of *argv[] which we see in main?
No. The only place that there is an exact equivalence between T ** and T
*[] is in a formal parameter declaration, and that's only for
hysterical reasons.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
mdh said:
Is this thus the equivalent of *argv[] which we see in main?
Richard Heathfield <r...@see.sig.invalidwrote:
No. The only place that there is an exact equivalence between T ** and T
*[] is in a formal parameter declaration, and that's only for
hysterical reasons.
Thanks.
mdh wrote:
On Apr 17, 6:59 pm, Ian Collins <ian-n...@hotmail.comwrote:
mdh wrote:
given:
char *s[]={"Jan","Feb","Mar","April"};
It might be conceptually easier to think of s as a char**.
Then you want p to be another char**.
char** p = s;
for( unsigned n = 0; n < 4; ++n )
{
printf("%s", *p );
++p;
Ian...got "JanJanJan"
if I replaced *p with *p++ got as expected.
Does this make sense?
Cut and paste the exact (complete minimal) program you ran. It should
not have done that. A good bet would be you forgot the braces, so you
ended up with:
for( unsigned n = 0; n < 4; ++n )
printf("%s", *p );
++p;
Brian
mdh wrote:
And I thought I understood it, finally. Alas.
given:
char *s[]={"Jan","Feb","Mar","April"};
is it possible to have char *p point at s?
*p = s...does not do it, as I expect.
*p=s[0]...I believe sets it to point to the first element in s. But
this is not what I want. What I want is a pointer which, when
incremented will produce "Jan","Feb","Mar","April" etc.
I believe what I want is a pointer equivalent of:
printf("%s", s[i]), (where i = 0->3)
#include <stdio.h>
#include <string.h>
int main(void)
{
char *s[] = { "Jan", "Feb", "Mar", "April" };
char *p, **q, *endp;
size_t ns = sizeof s / sizeof *s, i;
printf("[Output for one implementation:\n\n");
printf("The original array has these contents\n");
for (i = 0; i < ns; i++)
printf("s[%zu] @ %p: \"%s\"\n", i, (void *) s[i], s[i]);
putchar('\n');
printf("p is a pointer to char, so incrementing it leads\n"
"to changes of 1 byte. For example, with\n"
"initialization of p=s[0], we can get:\n");
for (p = s[0], endp = strchr(s[0], 0); p <= endp; p++)
printf("@ %p: %o %c\n", (void *) p, *p, *p ? *p : ' ');
putchar('\n');
printf("q is a pointer to pointer to char, so incrementing it\n"
"leads to changes of 1 pointer. For example, with\n"
"initialization of q=s, we can get:\n");
for (q = s; q <= &s[ns - 1]; q++)
printf("q = %p, *q = %p: \"%s\"\n", (void *) q, (void *) *q,
*q);
putchar('\n');
return 0;
}
[Output for one implementation:
The original array has these contents
s[0] @ 1df0: "Jan"
s[1] @ 1df4: "Feb"
s[2] @ 1df8: "Mar"
s[3] @ 1dfc: "April"
p is a pointer to char, so incrementing it leads
to changes of 1 byte. For example, with
initialization of p=s[0], we can get:
@ 1df0: 112 J
@ 1df1: 141 a
@ 1df2: 156 n
@ 1df3: 0
q is a pointer to pointer to char, so incrementing it
leads to changes of 1 pointer. For example, with
initialization of q=s, we can get:
q = dff98, *q = 1df0: "Jan"
q = dff9c, *q = 1df4: "Feb"
q = dffa0, *q = 1df8: "Mar"
q = dffa4, *q = 1dfc: "April"
On Apr 17, 10:16 pm, Martin Ambuhl <mamb...@earthlink.netwrote:
>The original array has these contents
s[0] @ 1df0: "Jan"
s[1] @ 1df4: "Feb"
s[2] @ 1df8: "Mar"
s[3] @ 1dfc: "April"
p is a pointer to char, so incrementing it leads
to changes of 1 byte. For example, with
initialization of p=s[0], we can get:
@ 1df0: 112 J
@ 1df1: 141 a
@ 1df2: 156 n
@ 1df3: 0
q is a pointer to pointer to char, so incrementing it
leads to changes of 1 pointer. For example, with
initialization of q=s, we can get:
q = dff98, *q = 1df0: "Jan"
q = dff9c, *q = 1df4: "Feb"
q = dffa0, *q = 1df8: "Mar"
q = dffa4, *q = 1dfc: "April"
Thank You Martin,
It's beginning to make a lot more sense now.
On Apr 17, 10:16 pm, Martin Ambuhl <mamb...@earthlink.netwrote:
>
p is a pointer to char, so incrementing it leads
q is a pointer to pointer to char, so incrementing it
Martin,
Could you clear up one last confusing issue...( well, I guess, not
last, but confusing :-))
The reason this whole question arose was to understand *argv[] (in
main) and try and create an analagous situation to it.
On p 114 (K&R) it says *argv[].....is a pointer to an array of
character strings"
As I asked somewhat cryptically earlier, (which was answered), just to
clarify, where does this fit in? I would guess that *argv[] under this
circumstance is type pointer to pointer to char? Which, if this is
true, should also be allowed to be written as **argv ? Not sure why it
is not thus written then.
Once again...thanks for taking the time to clear up something which I
guess all newbies struggle with.
mdh wrote:
>
The reason this whole question arose was to understand *argv[] (in
main) and try and create an analagous situation to it.
On p 114 (K&R) it says *argv[].....is a pointer to an array of
character strings"
As I asked somewhat cryptically earlier, (which was answered), just to
clarify, where does this fit in? I would guess that *argv[] under this
circumstance is type pointer to pointer to char? Which, if this is
true, should also be allowed to be written as **argv ? Not sure why it
is not thus written then.
It often is. It's certainly my preferred form.
--
Ian Collins.
mdh wrote:
I would guess that *argv[] under this
circumstance is type pointer to pointer to char? Which, if this is
true, should also be allowed to be written as **argv ? Not sure why it
is not thus written then.
On Apr 18, 12:40 am, Ian Collins <ian-n...@hotmail.comwrote:
>
It often is. It's certainly my preferred form.
Ok...well that clears that up...thank you. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Gerald |
last post by:
I have a problem with an array of pointers.
In a program I'm writing, I have to read a file, containing thousands
of short lines. The content of another file will be compared against
each line...
|
by: Steve |
last post by:
I want an initializer for an array of pointers to arrays of strings.
So I can do something like this:
const char *t1 = { "a", "b", "c", NULL };
const char *t2 = { "p", "q", NULL };
const char...
|
by: Peter B. Steiger |
last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C
is a simple word game that involves stringing together random lists of
words. In the Pascal version the whole array was...
|
by: gaga |
last post by:
I can't seem to get this to work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *names;
char **np;
|
by: Frank M. |
last post by:
I'm trying to declare an array of pointers to structures so that I can make
the last element a NULL pointer. I figure that it would more easily allow
my library routines to know when to stop...
| |
by: ptek |
last post by:
Hi all,
I'm quite new to pointers, so this might be a silly question :S
I need to allocate an array of pointers to unsigned char type...
So, if I needed instead to allocate an array of...
|
by: Piotrek |
last post by:
Hi there again!
Last time you helped me with pointers - it let me to save many hours of
searching for some solutions. And once again I have question.
Let's declare array of pointers:
char...
|
by: Jess |
last post by:
Hi,
If I have an array of pointer like:
char* a = {"a","b","c"};
then it works fine. Since "a" is effectively "a" char**, I tried the
following, which doesn't work:
char** a =...
|
by: ramu |
last post by:
Hi,
Could anyone please tell me how to dereference a pointer to an
array of pointers?
Regards
|
by: Slain |
last post by:
I have more of a conceptual question now. Let us say I do this:-
char *str; --create an array of pointers
str= "John";
I thought this would automatically put John at some memory space and...
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |