Re: Arrays of pointers to strings?



Joe,

I should of thought about it. It makes sence!

I appreciate your help and thanks alot !

--
Best regards
Robert


"Joe" wrote:


"Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:AE3541E9-1FAF-4C90-83DB-A505EA66B917@xxxxxxxxxxxxxxxx
Hi,

I have tried the following 'C' code:

===============================
char *p[]=
{
"Input ",
"Output ",
"Printer ",
"PaperOut ",
"DiskFull ",
"Write ",
""
};
//Display the first character only
for(i=0; *p[i]; i++)
printf("%c\n",*p[i]);

This:

printf("%c\n",*p[i]);


Is the same as this:

printf("%c\n", p[i][0]);

============================

This displays the first character of every string:

I
O
P
P
D
W

What if I wanted to display the 3rd character of every string instead?

To print the 3rd character:

printf("%c\n", p[i][2]);

Which is the same as this:

printf("%c\n",*(p[i]+2));


I have tried to turn it in a 2 or 3 dimensional array but no luck. I keep
getting errors. I have tried a dozen of variations and I don't know what
else
to try anymore.

Can someone please tell me what I am doing wrong.

It may be easier to see what is happening by splitting out the "get the
address of the right string" from the "display a particular character of a
string".

char* s = p[i]; // get the right string
printf("%c\n",*s); // print first character
printf("%c\n",*(s+0)); // print first character
printf("%c\n",*(s+1)); // print second character
printf("%c\n",*(s+2)); // print third character



--
Best regards
Robert



.



Relevant Pages

  • Re: Interating over the characters in a string
    ... I have a question in regards to .Net string maniplulation. ... The problem is I have a CSV parser that will successfully parse out quoted ... pairs to reprisent a single character. ...
    (microsoft.public.dotnet.framework)
  • Re: how to convert xf0 to 0xf0 ?
    ... various lengths into the appropriate Python types, ... directly, but in python, it is a string. ... because the actual character is not printable: ...
    (comp.lang.python)
  • Re: How to output ascii code to serial port?
    ... If you need this in a string, \005 or \x05 will be the string ... The syntax for a character in a string, single-or-double-quoted, is one of several forms ... I am trying to output ascii codes of non-printing characters to a serial ...
    (microsoft.public.vc.mfc)
  • Interating over the characters in a string
    ... I have a question in regards to .Net string maniplulation. ... pairs to reprisent a single character. ...
    (microsoft.public.dotnet.framework)
  • Re: Interating over the characters in a string
    ... I have a question in regards to .Net string maniplulation. ... > question in regards to interating over individual characters in a string. ... > pairs to reprisent a single character. ...
    (microsoft.public.dotnet.framework)

Quantcast