Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer

Reply
Thread Tools

pointer

 
 
vim
Guest
Posts: n/a
 
      05-18-2006
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.

 
Reply With Quote
 
 
 
 
vim
Guest
Posts: n/a
 
      05-18-2006
Please another thing is there is statement after
char **p1;
p1=p;

 
Reply With Quote
 
 
 
 
Vladimir Oka
Guest
Posts: n/a
 
      05-18-2006

vim wrote:
> 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[i][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).

 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      05-18-2006

vim wrote:
> Please another thing is there is statement after
> char **p1;
> p1=p;


Quote context, even when replying to own posts. Read
<http://cfaj.freeshell.org/google/>.

I have a sneaky suspicion you've been told this before (and more than
once). If you want to be taken seriously here, please follow the advice
in the link above, and also below:

<http://www.clc-wiki.net/wiki/Introduction_to_comp.lang.c>

 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      05-18-2006

"vim" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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?
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      05-18-2006

Vladimir Oka wrote:
> vim wrote:
> > 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 <ctype.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[i][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).


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-18-2006
Vladimir Oka wrote:
>
> vim wrote:
> > 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[i][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 */


--
pete
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      05-18-2006
On 2006-05-18, vim <(E-Mail Removed)> wrote:
> hi guys
>

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[i]), 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.

--

Andrew Poelstra < http://www.wpsoftware.net/blog >
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-18-2006
pete wrote:
>
> Vladimir Oka wrote:
> >
> > vim wrote:
> > > hi guys


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.

--
pete
 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      05-18-2006

pete wrote:
> Vladimir Oka wrote:
> >
> > vim wrote:
> > > hi guys
> > >
> > > this is my code
> > > #include<stdio.h>
> > > main()


<snip>

> > 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"


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

<snip>

> > 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.


It was deliberate. I wasn't going to do OP's homework.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments