![]() |
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 |
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. |
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 |
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 |
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. |
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> |
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. |
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. |
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! |
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.