Segmentation fault char array pointer

Discussion in 'C Programming' started by esanchezfo, Jun 1, 2006.

  1. esanchezfo

    esanchezfo Guest

    Please tell me what is wrong, what is the cause of the segmentation?

    #include <stdio.h>
    #include <malloc.h>



    void GetBuffer (unsigned char ** buffer)
    {
    * buffer = malloc (sizeof (unsigned char [3]));

    * buffer [0] = 0x00; // Segmentation Fault
    * buffer [1] = 0xA0;
    * buffer [2] = 0xB0;

    //printf ("Resultado de GetBuffer Hexa 0: %x, %x, %x\n", * buffer [0],
    * buffer [1], * buffer [2]);
    }

    void ShowBuffer (unsigned char * buffer)
    {
    //printf ("Resultado de GetBuffer Hexa II: %x, %x, %x, %x, %x\n",
    *buffer, buffer, buffer [0], buffer [1], buffer [2]);
    }


    int main ()
    {
    unsigned char * buffer = NULL;

    GetBuffer (&buffer);

    //printf ("Resultado de GetBuffer Hexa I: %x, %x, %x\n", buffer [0],
    buffer [1], buffer [2]);
    //printf ("Resultado de GetBuffer Hexa II: %x, %x, %x\n", buffer [0],
    buffer [1], buffer [2]);
    //printf ("Resultado de GetBuffer Hexa III: %x, %x, %x\n", buffer [0],
    buffer [1], buffer [2]);
    //printf ("Direccion de &buffer I: %x, %x\n", &buffer, buffer [0]);
    //ShowBuffer (buffer);

    return 0;
    }


    Thanks a lot.
     
    esanchezfo, Jun 1, 2006
    #1
    1. Advertising

  2. "esanchezfo" <> wrote in message
    news:...
    > Please tell me what is wrong, what is the cause of the segmentation?
    >
    > #include <stdio.h>
    > #include <malloc.h>
    >
    >
    >
    > void GetBuffer (unsigned char ** buffer)
    > {
    > * buffer = malloc (sizeof (unsigned char [3]));
    >
    > * buffer [0] = 0x00; // Segmentation Fault


    Precedence of operators has bitten you
    Use (*buffer)[0]

    > * buffer [1] = 0xA0;
    > * buffer [2] = 0xB0;
    >
    > //printf ("Resultado de GetBuffer Hexa 0: %x, %x, %x\n", * buffer [0],
    > * buffer [1], * buffer [2]);
    > }
    >
    > void ShowBuffer (unsigned char * buffer)
    > {
    > //printf ("Resultado de GetBuffer Hexa II: %x, %x, %x, %x, %x\n",
    > *buffer, buffer, buffer [0], buffer [1], buffer [2]);
    > }
    >
    >
    > int main ()
    > {
    > unsigned char * buffer = NULL;
    >
    > GetBuffer (&buffer);
    >
    > //printf ("Resultado de GetBuffer Hexa I: %x, %x, %x\n", buffer [0],
    > buffer [1], buffer [2]);
    > //printf ("Resultado de GetBuffer Hexa II: %x, %x, %x\n", buffer [0],
    > buffer [1], buffer [2]);
    > //printf ("Resultado de GetBuffer Hexa III: %x, %x, %x\n", buffer [0],
    > buffer [1], buffer [2]);
    > //printf ("Direccion de &buffer I: %x, %x\n", &buffer, buffer [0]);
    > //ShowBuffer (buffer);
    >
    > return 0;
    > }
    >
    >
    > Thanks a lot.
    >

    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
     
    Fred Kleinschmidt, Jun 1, 2006
    #2
    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. Goh, Yong Kwang
    Replies:
    3
    Views:
    11,883
    Barry Schwarz
    Apr 3, 2004
  2. Chris Van Extergem

    Function pointer results in segmentation fault.

    Chris Van Extergem, May 1, 2006, in forum: C Programming
    Replies:
    5
    Views:
    888
    Chris Van Extergem
    May 2, 2006
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,155
    Ian Collins
    May 9, 2006
  4. Ben
    Replies:
    8
    Views:
    488
  5. asit
    Replies:
    8
    Views:
    426
    Martin Ambuhl
    Jan 13, 2008
Loading...

Share This Page