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

pointer

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.

May 18 '06 #1
14 2019
vim
Please another thing is there is statement after
char **p1;
p1=p;

May 18 '06 #2

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

May 18 '06 #3

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>

May 18 '06 #4

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


May 18 '06 #5
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
May 18 '06 #6
On 2006-05-18, vim <vg*****@gmail.com> 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 >
May 18 '06 #7
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
May 18 '06 #8

"vim" <vg*****@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr 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
May 18 '06 #9

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

May 18 '06 #10
Andrew Poelstra wrote:
On 2006-05-18, vim <vg*****@gmail.com> wrote:

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


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

--
Thad
May 20 '06 #11
On Thu, 18 May 2006 14:42:27 GMT, Andrew Poelstra
<ap*******@localhost.localdomain> wrote:
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
May 29 '06 #12
On 2006-05-29, Dave Thompson <da*************@worldnet.att.net> wrote:
On Thu, 18 May 2006 14:42:27 GMT, Andrew Poelstra
<ap*******@localhost.localdomain> wrote:
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.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
It's just like stealing teeth from a baby.
May 29 '06 #13
Andrew Poelstra wrote:
On 2006-05-29, Dave Thompson <da*************@worldnet.att.net> wrote:
On Thu, 18 May 2006 14:42:27 GMT, Andrew Poelstra
<ap*******@localhost.localdomain> wrote:
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 */

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
May 29 '06 #14
On 2006-05-29, Joe Wright <jo********@comcast.net> wrote:
Andrew Poelstra wrote:
On 2006-05-29, Dave Thompson <da*************@worldnet.att.net> wrote:
On Thu, 18 May 2006 14:42:27 GMT, Andrew Poelstra
<ap*******@localhost.localdomain> wrote:

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.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
It's just like stealing teeth from a baby.
May 29 '06 #15

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;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.