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" <> wrote in
    news::

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


    No storage has been allocated for the 5 characters you are trying to copy
    to kernel_buf. When you initialize the pointer with a constant string
    for user_buf the compiler allocates storage for the string. Don't make
    the error of thinking that you may simply allocate space for kernel_buf
    by initializing it with a string 5 chars in length. You can not safely
    modify the string constant created through initialization of pointer to
    char.

    You must allocate storage... either via array notation:

    char kernel_buf[6] // 5 + 1 char for termination!

    or dynamic allocation using malloc:

    #define BUFF_SIZE
    char * kernel_buf = malloc(BUFF_SIZE+1);

    -rs
    Roger Scudder, Oct 17, 2006
    #2
    1. Advertising

  3. priyanka

    Uncle_Jed Guest

    "priyanka" <> wrote in
    news::

    Also note...

    > *kernel_buf = *(user_buf++); // need to increment kernel_buf here


    Better to do it like this...

    kernel_buf = user_buf;

    but

    *kernel_buf++ = *user_buf++;

    will work.

    -rs
    Uncle_Jed, Oct 17, 2006
    #3
    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:
    511
  2. Marc Schellens

    Iterator/pointer arithmetic

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

    pointer arithmetic help

    priyanka, Oct 17, 2006, in forum: C Programming
    Replies:
    14
    Views:
    484
    Joe Wright
    Oct 19, 2006
  5. priyanka

    pointer arithmetic help

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

Share This Page