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. In article <>,
    priyanka <> wrote:

    >I need to copy the first 5 characters stored in a buffer into another
    >buffer one character at a time.


    Sounds like an assignment. But you provided your code to show what you
    have tried so far, so we will try to assist you.


    >#include<stdio.h>


    >#define LENGTH 5
    >char * kernel_buf;


    Note that the above is a variable at "file scope". That will be important
    later.

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


    >int main(){


    As a matter of style, it is generally recommended these days that if
    you do not have any arguments to main, that you use main(void) instead
    of main() -- e.g.,

    int main(void){

    > int i;
    > char ch;


    You do not use ch in your program, so that could be removed.

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

    One of the non-obvious bits of the C language is that variables
    at "file scope" are global variables, and are automatically initialized
    to the value 0 unless they are initialized to something else. So
    kernel_buf (the pointer) was initialized to 0, which is the same,
    according to the C standards, as initializing it to NULL . Thus
    kernel_buf starts at as NULL and so the first thing you try to
    do is write to memory at the NULL pointer.

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


    You also have not changed the pointer kernel_buf, so you would end
    up writing all of the characters to the -same- location.

    > return 1;


    Returning 1 does not have any particular meaning as far as C
    is concerned. Returning 0 is defined to indicate a successful finish.
    If you want to indicate an unsuccesful finish, the best thing to do is
    to #include <stdlib.h> and return EXIT_FAILURE which will be some
    value your operating system will interpret as indicating a failure.

    >}



    Technical note: in the long run, it is better to use size_t instead
    of int for indices -- e.g., size_t i; instead of int i;
    size_t is some type which is big enough to index any array that the
    implementation will allow you to access, whereas int might not be
    big enough for that. For example there are a number of systems on which
    the type 'int' is only 16 bits, and can have a maximum value of
    32767; on those systems, if you needed to copy more than 32767
    characters, your 'int' value i would not be big enough to hold
    the number.
    --
    If you lie to the compiler, it will get its revenge. -- Henry Spencer
    Walter Roberson, Oct 17, 2006
    #2
    1. Advertising

  3. priyanka

    Thad Smith Guest

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


    You haven't defined a buffer to copy the characters into. The easiest
    way to do this is to use an array, rather than a character pointer.

    char kernel_buf[LENGTH+1]; /* contains an extra element for
    null terminator */

    Then use i to specify which character location to write:
    kernel_buf = *user_buf++;
    or better
    kernel_buf = user_buf; /* doesn't change user_buf */

    then
    printf ("%c\n", kernel_buf);
    if you want to print a single character at a time, each on a new line
    or after the copy loop, you can write
    printf ("%s\n", kernel_buf);
    which will print all the copied characters at once (but without
    intervening new lines).

    When you use a pointer, you must make sure that it points to the correct
    location for fetching or storing.

    The second method above of copying is normally preferred, since the
    first method modifies user_buf, which doesn't allow you to reuse it.
    Your current program doesn't need to reuse it, but normally you want to
    keep an unmodified version of your pointer.

    That brings up a second point: you could also define user_buf as an
    array. I find this a more natural use of C, although your way works as
    well.
    const char user_buf[] = "Priya is enlightened\n";


    --
    Thad
    Thad Smith, 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:
    507
  2. Marc Schellens

    Iterator/pointer arithmetic

    Marc Schellens, Dec 5, 2003, in forum: C++
    Replies:
    15
    Views:
    832
    tom_usenet
    Dec 8, 2003
  3. joshc
    Replies:
    5
    Views:
    533
    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:
    267
    Uncle_Jed
    Oct 17, 2006
Loading...

Share This Page