Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   pointer arithmetic help (http://www.velocityreviews.com/forums/t444769-pointer-arithmetic-help.html)

priyanka 10-17-2006 12:09 AM

pointer arithmetic help
 
Hi there,

I need to copy the first 5 characters stored in a buffer into another
buffer one character at a time. I tried doiing it as under but I got
segmentation errors:
#include<stdio.h>

#define LENGTH 5
char * kernel_buf;
char * user_buf = "Priya is confused\n";

int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
printf("%c\n",*kernel_buf);
}
return 1;
}

The message I get is:
user_buf:Priya is confused
Segmentation fault

If anyone could help me figure out where I am wrong it would be very
helpful.

Thank you,
Priya


Ian Collins 10-17-2006 12:26 AM

Re: pointer arithmetic help
 
priyanka wrote:
> Hi there,
>
> I need to copy the first 5 characters stored in a buffer into another
> buffer one character at a time. I tried doiing it as under but I got
> segmentation errors:


Posting three times isn't a good idea. Usenet isn't synchronous, you
have to wait for posting to propagate.

> #include<stdio.h>
>
> #define LENGTH 5
> char * kernel_buf;
> char * user_buf = "Priya is confused\n";
>

Make the second const char*, you can't change it.

> int main(){
> int i;
> char ch;
> printf("user_buf:%s",user_buf);
> for(i = 0; i < LENGTH; i++){
> *kernel_buf = *(user_buf++);


You haven't allocated any memory for kernel_buf. See malloc().

Once that is fixed, you'll probably want something like

kernel_buf[i] = user_buf[i];

--
Ian Collins.

Chris Johnson 10-17-2006 01:18 AM

Re: pointer arithmetic help
 

priyanka wrote:
> Hi there,
>
> I need to copy the first 5 characters stored in a buffer into another
> buffer one character at a time. I tried doiing it as under but I got
> segmentation errors:
>
> #include<stdio.h>
>
> #define LENGTH 5
> char * kernel_buf;


Note that this is a char pointer, and by default is likely set to NULL.

> char * user_buf = "Priya is confused\n";


This you've actually given a value, so the user_buf holds the address
of the string "Priya is confused\n".

> int main(){
> int i;
> char ch;
> printf("user_buf:%s",user_buf);
> for(i = 0; i < LENGTH; i++){
> *kernel_buf = *(user_buf++);


What is the benefit to using a pointer to a char instead of a char, in
this scenario? You are making things overly complex, and have ended up
attempting to store a value at an invalid address. (Incidentally, this
is where your segfault will be.)

There are several solutions. You could declare kernel_buf to simply be
a char, and get rid of all your asterisks. You could malloc a char. Or
you could set kernel_buf to point to the char ch you have above.

i.e.

char kernel_buf;
for(i = 0; i < LENGTH; i++){
kernel_buf = *user_buf++;
printf("%c\n",kernel_buf);
}

or

kernel_buf = malloc(sizeof(char));
for(i = 0; i < LENGTH; i++){
*kernel_buf = *user_buf++;
printf("%c\n",*kernel_buf);
}

or

kernel_buf = &ch;
for(i = 0; i < LENGTH; i++){
*kernel_buf = *user_buf++;
printf("%c\n",*kernel_buf);
}

> printf("%c\n",*kernel_buf);
> }
> return 1;


Generally, you return 0 on success.

> }
>
> The message I get is:
> user_buf:Priya is confused
> Segmentation fault
>
> If anyone could help me figure out where I am wrong it would be very
> helpful.
>
> Thank you,
> Priya



priyanka 10-17-2006 01:34 AM

Re: pointer arithmetic help
 
Thank you for your help. I tried according to what you guys told:
#include<stdio.h>

#define LENGTH 5

char *kernel_buf;
char *user_buf = "Priya is confused\n";
kernel_buf = malloc(sizeof(char));

int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
printf("%c\n",*kernel_buf);
}
return 1;
}

Now I got error telling that there are conflicting types for
kernel_buf.

Thank you,
Priya

On Oct 16, 6:18 pm, "Chris Johnson" <effig...@gmail.com> wrote:
> priyanka wrote:
> > Hi there,

>
> > I need to copy the first 5 characters stored in a buffer into another
> > buffer one character at a time. I tried doiing it as under but I got
> > segmentation errors:

>
> > #include<stdio.h>

>
> > #define LENGTH 5
> > char * kernel_buf;Note that this is a char pointer, and by default is likely set to NULL.

>
> > char * user_buf = "Priya is confused\n";This you've actually given a value, so the user_buf holds the address

> of the string "Priya is confused\n".
>
> > int main(){
> > int i;
> > char ch;
> > printf("user_buf:%s",user_buf);
> > for(i = 0; i < LENGTH; i++){
> > *kernel_buf = *(user_buf++);What is the benefit to using a pointer to a char instead of a char, in

> this scenario? You are making things overly complex, and have ended up
> attempting to store a value at an invalid address. (Incidentally, this
> is where your segfault will be.)
>
> There are several solutions. You could declare kernel_buf to simply be
> a char, and get rid of all your asterisks. You could malloc a char. Or
> you could set kernel_buf to point to the char ch you have above.
>
> i.e.
>
> char kernel_buf;
> for(i = 0; i < LENGTH; i++){
> kernel_buf = *user_buf++;
> printf("%c\n",kernel_buf);
>
> }or
>
> kernel_buf = malloc(sizeof(char));
> for(i = 0; i < LENGTH; i++){
> *kernel_buf = *user_buf++;
> printf("%c\n",*kernel_buf);
>
> }or
>
> kernel_buf = &ch;
> for(i = 0; i < LENGTH; i++){
> *kernel_buf = *user_buf++;
> printf("%c\n",*kernel_buf);
>
> }
> > printf("%c\n",*kernel_buf);
> > }
> > return 1;Generally, you return 0 on success.

>
> > }

>
> > The message I get is:
> > user_buf:Priya is confused
> > Segmentation fault

>
> > If anyone could help me figure out where I am wrong it would be very
> > helpful.

>
> > Thank you,
> > Priya



Ian Collins 10-17-2006 01:38 AM

Re: pointer arithmetic help
 
priyanka wrote:
> Thank you for your help. I tried according to what you guys told:


Please don't top post, your reply should follow the message you are
relying to.

> #include<stdio.h>
>
> #define LENGTH 5
>
> char *kernel_buf;
> char *user_buf = "Priya is confused\n";
> kernel_buf = malloc(sizeof(char));
>

This only assigns a buffer of one character to kernel_buf. You should
have this assignment (and the declaration) within main().

> int main(){
> int i;
> char ch;
> printf("user_buf:%s",user_buf);
> for(i = 0; i < LENGTH; i++){
> *kernel_buf = *(user_buf++);


You are still copying all the characters in user_buf to the first
character in kernel_buf.

--
Ian Collins.

Chris Johnson 10-17-2006 02:12 AM

Re: pointer arithmetic help
 
priyanka wrote:
> Thank you for your help. I tried according to what you guys told:
> #include<stdio.h>
>
> #define LENGTH 5
>
> char *kernel_buf;
> char *user_buf = "Priya is confused\n";
> kernel_buf = malloc(sizeof(char));


This should go in your main(). malloc() is a function, not a constant,
so it has to be called in a method.

I have to say I honestly didn't expect you to use malloc(). It's
generally used in allocating arrays, and is completely unnecessary
here. I think your better choice would be to drop the * on the initial
declaration (and later dereferences) of kernel_buf.

Also, I was just working with what the program indicated you were going
for, which was a single character buffer that iterated through the
first five characters of user_buf. Others seem to be under the
impression that you're wanting to store all five characters at once. If
that's the case, Ian Collins probably gave the most succinct
description of what you'd want to do there.

> int main(){
> int i;
> char ch;
> printf("user_buf:%s",user_buf);
> for(i = 0; i < LENGTH; i++){
> *kernel_buf = *(user_buf++);
> printf("%c\n",*kernel_buf);
> }
> return 1;
> }
>
> Now I got error telling that there are conflicting types for
> kernel_buf.


I expect it's because you did not include <stdlib.h>, where malloc is
usually defined, so your compiler is guessing what malloc(sizeof(char))
means.

> Thank you,
> Priya


In the future, reply beneath the context to which you are replying.

<snip>


sandy 10-17-2006 07:26 AM

Re: pointer arithmetic help
 

priyanka wrote:
> Hi there,
>
> I need to copy the first 5 characters stored in a buffer into another
> buffer one character at a time. I tried doiing it as under but I got
> segmentation errors:
> #include<stdio.h>
>
> #define LENGTH 5
> char * kernel_buf;
> char * user_buf = "Priya is confused\n";
>


char * user_buf = "Priya is confused\n";
replace the above line with,

char user_buf[ ] = "Priya is confused\n";

This will solve your complete problem.

> int main(){
> int i;
> char ch;
> printf("user_buf:%s",user_buf);
> for(i = 0; i < LENGTH; i++){
> *kernel_buf = *(user_buf++);
> printf("%c\n",*kernel_buf);
> }
> return 1;
> }
>
> The message I get is:
> user_buf:Priya is confused
> Segmentation fault
>
> If anyone could help me figure out where I am wrong it would be very
> helpful.
>
> Thank you,
> Priya


Cheers,
Sandeep.


sandy 10-17-2006 07:30 AM

Re: pointer arithmetic help
 

priyanka wrote:
> Hi there,
>
> I need to copy the first 5 characters stored in a buffer into another
> buffer one character at a time. I tried doiing it as under but I got
> segmentation errors:
> #include<stdio.h>
>
> #define LENGTH 5
> char * kernel_buf;

I forgot to mention, no memory is allocated for kernel_buf. So, do that
using malloc( ) and terminale the last character with a NUll, else, if
ever you try to print it using %s, you might again get a seg fault.
> char * user_buf = "Priya is confused\n";
>
> int main(){
> int i;
> char ch;
> printf("user_buf:%s",user_buf);
> for(i = 0; i < LENGTH; i++){
> *kernel_buf = *(user_buf++);
> printf("%c\n",*kernel_buf);
> }
> return 1;
> }
>
> The message I get is:
> user_buf:Priya is confused
> Segmentation fault
>
> If anyone could help me figure out where I am wrong it would be very
> helpful.
>
> Thank you,
> Priya


Cheers,
Sandeep.


Richard 10-17-2006 08:10 AM

Re: pointer arithmetic help
 
"priyanka" <priyankabhar@gmail.com> writes:

> Thank you for your help. I tried according to what you guys told:
> #include<stdio.h>
>
> #define LENGTH 5
>
> char *kernel_buf;
> char *user_buf = "Priya is confused\n";
> kernel_buf = malloc(sizeof(char));
>
> int main(){
> int i;
> char ch;
> printf("user_buf:%s",user_buf);
> for(i = 0; i < LENGTH; i++){
> *kernel_buf = *(user_buf++);
> printf("%c\n",*kernel_buf);
> }
> return 1;
> }
>
> Now I got error telling that there are conflicting types for
> kernel_buf.


Your malloc is in the wrong place. It needs to be in main.

For now, and for learning process as I think pointers are confusing you
a little : define kernel_buf as

char kernel_buf[LENGTH+1]; /* zero terminated to be on the safe side */

to start with.

Then in your loop:

kernel_buf[i]=user_buf[i];

After you see this working, maybe then make kernel_buf a local variable in main and try to use
malloc there - it will be back to defining kernel_buf as a "char *"
again. Different from the array notation I used above.

If you use a debugger, experiment with querying the system what value
kernel_buf has, what &kernel_buf[0] is, what *kernel_buf is etc etc -
you will get the hang of it! Most of all, read Kernighan & Ritchies
"The C Programming Language".

best of luck!


Default User 10-17-2006 04:46 PM

Re: pointer arithmetic help
 
sandy wrote:

>
> priyanka wrote:
> > Hi there,
> >
> > I need to copy the first 5 characters stored in a buffer into
> > another buffer one character at a time. I tried doiing it as under
> > but I got segmentation errors:
> > #include<stdio.h>
> >
> > #define LENGTH 5
> > char * kernel_buf;
> > char * user_buf = "Priya is confused\n";
> >

>
> char * user_buf = "Priya is confused\n";
> replace the above line with,
>
> char user_buf[ ] = "Priya is confused\n";
>
> This will solve your complete problem.


How will it have any affect? What is it that you think this solves? You
didn't correct the actual problem, and introduced a new one:

> > for(i = 0; i < LENGTH; i++){
> > *kernel_buf = *(user_buf++);


With your change, user_buf is no longer a pointer, and can't be
incremented.




Brian


All times are GMT. The time now is 01:05 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57