pointer arithmetic help

Discussion in 'C Programming' started by priyanka, Oct 17, 2006.

  1. priyanka

    priyanka Guest

    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
     
    priyanka, Oct 17, 2006
    #1
    1. Advertising

  2. priyanka

    Ian Collins Guest

    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 = user_buf;

    --
    Ian Collins.
     
    Ian Collins, Oct 17, 2006
    #2
    1. Advertising

  3. 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
     
    Chris Johnson, Oct 17, 2006
    #3
  4. priyanka

    priyanka Guest

    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" <> 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
     
    priyanka, Oct 17, 2006
    #4
  5. priyanka

    Ian Collins Guest

    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.
     
    Ian Collins, Oct 17, 2006
    #5
  6. 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>
     
    Chris Johnson, Oct 17, 2006
    #6
  7. priyanka

    sandy Guest

    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, Oct 17, 2006
    #7
  8. priyanka

    sandy Guest

    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.
     
    sandy, Oct 17, 2006
    #8
  9. priyanka

    Richard Guest

    "priyanka" <> 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=user_buf;

    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!
     
    Richard, Oct 17, 2006
    #9
  10. priyanka

    Default User Guest

    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
     
    Default User, Oct 17, 2006
    #10
  11. priyanka

    Simon Biber Guest

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


    Not just likely. It *must* be set to a null pointer.

    --
    Simon.
     
    Simon Biber, Oct 18, 2006
    #11
  12. Simon Biber wrote:
    > Chris Johnson 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.

    >
    > Not just likely. It *must* be set to a null pointer.


    Is this because it is declared at file level, or must all new
    unassigned pointers be set to null?
     
    Chris Johnson, Oct 18, 2006
    #12
  13. priyanka

    Bill Medland Guest

    Chris Johnson wrote:

    >
    > Simon Biber wrote:
    >> Chris Johnson 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.

    >>
    >> Not just likely. It *must* be set to a null pointer.

    >
    > Is this because it is declared at file level, or must all new
    > unassigned pointers be set to null?

    because it's at the file level.
    If it were a local variable in a function or block of code it would not be
    initialised.
    --
    Bill Medland
     
    Bill Medland, Oct 18, 2006
    #13
  14. Bill Medland <> writes:
    > Chris Johnson wrote:
    >> Simon Biber wrote:
    >>> Chris Johnson wrote:
    >>> > priyanka wrote:
    >>> >> 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.
    >>>
    >>> Not just likely. It *must* be set to a null pointer.

    >>
    >> Is this because it is declared at file level, or must all new
    >> unassigned pointers be set to null?

    > because it's at the file level.
    > If it were a local variable in a function or block of code it would not be
    > initialised.


    It's because it has static storage duration. That's the default for
    objects declared outside any function. Inside a function, it's too
    dark to read -- oop, wrong joke. Inside a function, an object can be
    made static by declaring it with the "static" keyword; in that case,
    it's initialized as if it were declared at file scope.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 18, 2006
    #14
  15. priyanka

    Joe Wright Guest

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

    I note that kernel_buff, at file scope, is initialized by the compiler
    to NULL, and is left unchanged by your program.

    Dereferencing NULL pointer is a no no.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Oct 19, 2006
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. a

    pointer arithmetic

    a, Sep 11, 2003, in forum: C++
    Replies:
    6
    Views:
    524
  2. Marc Schellens

    Iterator/pointer arithmetic

    Marc Schellens, Dec 5, 2003, in forum: C++
    Replies:
    15
    Views:
    848
    tom_usenet
    Dec 8, 2003
  3. joshc
    Replies:
    5
    Views:
    559
    Keith Thompson
    Mar 31, 2005
  4. priyanka

    pointer arithmetic help

    priyanka, Oct 17, 2006, in forum: C Programming
    Replies:
    2
    Views:
    293
    Thad Smith
    Oct 17, 2006
  5. priyanka

    pointer arithmetic help

    priyanka, Oct 17, 2006, in forum: C Programming
    Replies:
    2
    Views:
    274
    Uncle_Jed
    Oct 17, 2006
Loading...

Share This Page