pointer

V

vim

hi guys

this is my code
#include<stdio.h>
main()
{
char *p[]={"hello" ,"world" ,"Nice","show"};
char **p1;
}
I want to print the string "helwornicsho" by using ptr increment or
decrement.
How can I do that.
I tried to print charachter by charachter .
But for that increment ptr pointing to each charachter .
I used (**p1)++.But it is giving segmentation fault.
Plz tell the error.
 
V

Vladimir Oka

vim said:
hi guys

this is my code
#include<stdio.h>
main()
{
char *p[]={"hello" ,"world" ,"Nice","show"};
char **p1;
}
I want to print the string "helwornicsho" by using ptr increment or
decrement.
How can I do that.
I tried to print charachter by charachter .
But for that increment ptr pointing to each charachter .
I used (**p1)++.But it is giving segmentation fault.
Plz tell the error.

If you can't explain what exactly you want to achieve, and if you won't
post the code you tried and failed, there's little chance anyone can
help.

I guess you want to print first three characters of each element of
`p`, possibly with the first one of each being forced to lowercase. In
which case, this should do you nicely:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *p[]={"hello" ,"world" ,"Nice","show"};
int i,j;

for (i = 0; i < (sizeof p / sizeof p[0]); ++i)
{
/* no error checking here */
for (j = 0; j < 3; ++j)
{
printf("%c", tolower(p[j]));
}
}
printf("\n");

return 0;
}

Be warned, I did not include checks to see whether any of the array
elements are long enough (i.e. have at least three characters).
 
F

Fred Kleinschmidt

vim said:
Please another thing is there is statement after
char **p1;
p1=p;
Where is the context? I see two statements here; where is the rest?
What is your question?
 
V

Vladimir Oka

Vladimir said:
vim said:
hi guys

this is my code
#include<stdio.h>
main()
{
char *p[]={"hello" ,"world" ,"Nice","show"};
char **p1;
}
I want to print the string "helwornicsho" by using ptr increment or
decrement.
How can I do that.
I tried to print charachter by charachter .
But for that increment ptr pointing to each charachter .
I used (**p1)++.But it is giving segmentation fault.
Plz tell the error.

If you can't explain what exactly you want to achieve, and if you won't
post the code you tried and failed, there's little chance anyone can
help.

I guess you want to print first three characters of each element of
`p`, possibly with the first one of each being forced to lowercase. In
which case, this should do you nicely:

#include <stdio.h>
#include <stdlib.h>

Sorry, it should have been:

#include <stdio.h>
#include said:
int main(void)
{
char *p[]={"hello" ,"world" ,"Nice","show"};
int i,j;

for (i = 0; i < (sizeof p / sizeof p[0]); ++i)
{
/* no error checking here */
for (j = 0; j < 3; ++j)
{
printf("%c", tolower(p[j]));
}
}
printf("\n");

return 0;
}

Be warned, I did not include checks to see whether any of the array
elements are long enough (i.e. have at least three characters).
 
P

pete

Vladimir said:
hi guys

this is my code
#include<stdio.h>
main()
{
char *p[]={"hello" ,"world" ,"Nice","show"};
char **p1;
}
I want to print the string
"helwornicsho" by using ptr increment or decrement.
How can I do that.
I guess you want to print first three characters of each element of
`p`, possibly with the first one of each being forced to lowercase. In
which case, this should do you nicely:

#include <stdio.h>
#include <stdlib.h>

I think you mean "ctype" instead of "stdlib"
int main(void)
{
char *p[]={"hello" ,"world" ,"Nice","show"};
int i,j;

for (i = 0; i < (sizeof p / sizeof p[0]); ++i)
{
/* no error checking here */
for (j = 0; j < 3; ++j)
{
printf("%c", tolower(p[j]));
}
}
printf("\n");

return 0;
}

Be warned, I did not include checks to see whether any of the array
elements are long enough (i.e. have at least three characters).


It could be a little more pointer incremental.

/* BEGIN new.c */

#include <stdio.h>
#include <ctype.h>

#define THREE 3

int main(void)
{
char *p[] = {"hello" ,"world" ,"Nice","show"};
char **p1;
int x;

for (p1 = p; *p1 != p[sizeof p / sizeof *p]; ++p1) {
for (x = 0; x != THREE; ++x) {
putchar(tolower(*(*p1)++));
}
}
putchar('\n');
return 0;
}

/* END new.c */
 
A

Andrew Poelstra

You are already annoying me with your lack of capitals.
this is my code
One sec, here's a cleaned up version:

#include <stdio.h>
int main(void)
{
char *p[]={"hello" ,"world" ,"Nice","show"};
char **p1;

p1 = p; /* As per your most recent post */
/* You should have posted context */

/* Actual code here */
return 0;
}
I want to print the string "helwornicsho" by using ptr increment or
decrement.
What is "ptr"? I don't see it in your code.
How can I do that.
I tried to print charachter by charachter .
Easier to set the fourth character of each string to '\0' and then
printf ("%s", p), no?
But for that increment ptr pointing to each charachter .
I used (**p1)++.But it is giving segmentation fault.

**p1 points to the letter h, which is part of a string literal. You
don't need to increment that, and if you could you'd end up with 'i'
instead of the next letter.

Use *p1++ to jump to the next word, and p1++ to jump to the next
letter. The parentheses are unnecessary.
Plz tell the error.
What does "plz" mean? That isn't in your code either.
 
P

pete

I forgot to mention,
anyone who does your homework for you, isn't being your friend.
/* BEGIN new.c */
/* END new.c */

But if you ask questions, they will be answered.
 
V

Vladimir Oka

said:
I think you mean "ctype" instead of "stdlib"

Yes. Thanks. (I did notice myself, as well, albeit too late).

said:
It could be a little more pointer incremental.

It was deliberate. I wasn't going to do OP's homework. ;-)
 
T

Thad Smith

Andrew said:
One sec, here's a cleaned up version:

#include <stdio.h>
int main(void)
{
char *p[]={"hello" ,"world" ,"Nice","show"};
char **p1;

p1 = p; /* As per your most recent post */
/* You should have posted context */

/* Actual code here */
return 0;
}

I want to print the string "helwornicsho" by using ptr increment or
decrement.
...
How can I do that.
I tried to print charachter by charachter .

Easier to set the fourth character of each string to '\0' and then
printf ("%s", p), no?


Easier, possibly, but resulting in undefined behavior if the strings are
string literals as defined above.
 
D

Dave Thompson

char *p[]={"hello" ,"world" ,"Nice","show"};
char **p1;

p1 = p; /* As per your most recent post */
/* You should have posted context */
Use *p1++ to jump to the next word, and p1++ to jump to the next
letter. The parentheses are unnecessary.
p1++ or ++p1 to jump to the next word and
(*p1)++ (parens necessary) or ++*p1 for next letter.

- David.Thompson1 at worldnet.att.net
 
A

Andrew Poelstra

char *p[]={"hello" ,"world" ,"Nice","show"};
char **p1;

p1 = p; /* As per your most recent post */
/* You should have posted context */
Use *p1++ to jump to the next word, and p1++ to jump to the next
letter. The parentheses are unnecessary.
p1++ or ++p1 to jump to the next word and
(*p1)++ (parens necessary) or ++*p1 for next letter.

- David.Thompson1 at worldnet.att.net

I tested my method in gcc. I think that you are correct
about the parentheses (for style reasons, at the very least),
but when I compiled the OP's code (which may have been
different from the example code quoted), p1++ moved to the
next letter, not word.
 
J

Joe Wright

Andrew said:
char *p[]={"hello" ,"world" ,"Nice","show"};
char **p1;

p1 = p; /* As per your most recent post */
/* You should have posted context */
Use *p1++ to jump to the next word, and p1++ to jump to the next
letter. The parentheses are unnecessary.
p1++ or ++p1 to jump to the next word and
(*p1)++ (parens necessary) or ++*p1 for next letter.

- David.Thompson1 at worldnet.att.net

I tested my method in gcc. I think that you are correct
about the parentheses (for style reasons, at the very least),
but when I compiled the OP's code (which may have been
different from the example code quoted), p1++ moved to the
next letter, not word.
It's not for style. Remember p1 is a pointer which points to a pointer.
p1++ will advance p1 to the next pointer.

puts(*p1++); /* prints hello */
puts(*p1--); /* prints world */
puts(++(*p1)); /* prints ello */
 
A

Andrew Poelstra

Andrew said:
On Thu, 18 May 2006 14:42:27 GMT, Andrew Poelstra

char *p[]={"hello" ,"world" ,"Nice","show"};
char **p1;

p1 = p; /* As per your most recent post */
/* You should have posted context */
<snip>
Use *p1++ to jump to the next word, and p1++ to jump to the next
letter. The parentheses are unnecessary.

p1++ or ++p1 to jump to the next word and
(*p1)++ (parens necessary) or ++*p1 for next letter.

- David.Thompson1 at worldnet.att.net

I tested my method in gcc. I think that you are correct
about the parentheses (for style reasons, at the very least),
but when I compiled the OP's code (which may have been
different from the example code quoted), p1++ moved to the
next letter, not word.
It's not for style. Remember p1 is a pointer which points to a pointer.
p1++ will advance p1 to the next pointer.

puts(*p1++); /* prints hello */
puts(*p1--); /* prints world */
puts(++(*p1)); /* prints ello */
That is true for the example code quoted, but the OP's code behaved
differently. Oh well. I wasn't sure why it worked at the time, and
it has long since been deleted, so I can't check my work.

Anyway, I wasn't aware that parentheses were needed, so I learned
something very important today. (That bug has burned me several times,
and yet I never remember it). Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top