char* and char[]?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IanWright
    New Member
    • Jan 2008
    • 179

    char* and char[]?

    Just wondering if someone can clear things up for me a little. I'm fairly new to C/C++ and just getting in a bit of a tangle with pointers.

    My problem is I have a vector of a defined structure, which needs to store some characters. When I try and print out these characters later, it appears to be uninitialised. I believe the problem is because I'm using a char*, who's memory contents then just gets discarded later on, and hence I can't get the characters anymore.

    e.g.

    [CODE="cpp"]
    typedef struct _info{
    char* label;
    } Info;[/CODE]

    a little later on some parsing takes place...

    [CODE="cpp"]
    char *value;

    for (....)
    {
    char *label;
    Info info;
    label = value;
    info.label = label;

    // info gets added to a vector
    }
    [/CODE]

    When I come to print info later I get ▌▌▌▌▌▌▌▌▌▌

    Is my understanding of this correct? If so, then I guess that label within the Info structure needs to be a char[]. Is there a simple way to then copy the 'value' within the for loop into this char array as my attempts have just led to compiler errors... ?

    Thanks very much
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    I'm going to reply to my own question for the benefit of anyone else...

    I think explaining the problem is right, and the useful little way to do this is to use strcpy...

    [CODE="cpp"]
    char dest[50];
    char* pointer = "hello world";

    strcpy(dest, pointer);
    [/CODE]

    pointer can now be played around with freely without changing the value in dest.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      A char* is a pointer to a single char.

      A char[] is also a pointer to a char but you have to specify the char(s) or it won't compile:
      [code=c]
      char arr[] ="ABC"; /* OK. arr is an array of 4 char */
      char arr1[] = {'A', 'B', 'C'}; /* OK. arr1 is an array of 3 char */
      char arr2[]; /* ERROR: No char(s) specified.
      [/code]

      Read this:
      Originally posted by weaknessforcats
      First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
      [code=c]
      int array[5];
      [/code]

      That is an array of 5 elements each of which is an int.

      [code=c]
      int array[];
      [/code]

      won't compile. You need to declare the number of elements.

      Second, this array:
      [code=c]
      int array[5][10];
      [/code]

      is still an array of 5 elements. Each element is an array of 10 int.

      [code=c]
      int array[5][10][15];
      [/code]

      is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


      [code=c]
      int array[][10];
      [/code]

      won't compile. You need to declare the number of elements.

      Third, the name of an array is the address of element 0
      [code=c]
      int array[5];
      [/code]

      Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

      [code=c]
      int array[5][10];
      [/code]

      Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
      [code=c]
      int array[5][10];

      int (*ptr)[10] = array;
      [/code]

      Fourth, when the number of elements is not known at compile time, you create the array dynamically:

      [code=c]
      int* array = new int[value];
      int (*ptr)[10] = new int[value][10];
      int (*ptr)[10][15] = new int[value][10][15];
      [/code]

      In each case value is the number of elements. Any other brackets only describe the elements.

      Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

      [code=c]
      int** ptr = new int[value][10]; //ERROR
      [/code]

      new returns the address of an array of 10 int and that isn't the same as an int**.

      Likewise:
      [code=c]
      int*** ptr = new int[value][10][15]; //ERROR
      [/code]

      new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

      With the above in mind this array:
      [code=cpp]
      int array[10] = {0,1,2,3,4,5,6, 7,8,9};
      [/code]
      has a memory layout of

      0 1 2 3 4 5 6 7 8 9

      Wheras this array:
      [code=cpp]
      int array[5][2] = {0,1,2,3,4,5,6, 7,8,9};
      [/code]
      has a memory layout of

      0 1 2 3 4 5 6 7 8 9

      Kinda the same, right?

      So if your disc file contains

      0 1 2 3 4 5 6 7 8 9

      Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

      Therefore, when you do your read use the address of array[0][0] and read as though you have a
      one-dimensional array and the values will be in the correct locations.

      Comment

      • Rajesh V
        New Member
        • Dec 2007
        • 16

        #4
        In your post, you have said that

        Code:
        int array[][10];
        won't compile because we need to declare the number of elements.

        But, if i do like this,

        Code:
        int array[10][];
        The compiler gives the error - declaration of ‘a’ as multidimensiona l array must have bounds for all dimensions except the first

        Also, when i do like this,

        int array[][10] = { { 0 } };

        It gets compiled successfully.

        Can you please explain what's happening?

        Comment

        • Andr3w
          New Member
          • Nov 2007
          • 42

          #5
          Well, for the last used declariation with the zero you initialize the variable to be like this:

          [code=c]
          int index[1][10] = { 0 };
          [/code]

          Also you don't need a "duo" of curly brackets for that like you used... Anyway I think that weaknessforcats meant the following use:

          [code=c]
          #include <stdio.h>

          int Size(int index[][3]);
          int main(int argc, char **argv)
          {
          // the first dimention (currently has a value of 1 can be any value
          // the function will accept it, so it would be valid to write
          // index[10][3]. the function would be ok with it!
          int index[1][3];

          Size(index);
          return 0;
          }

          int Size(int index[][3])
          {
          return;
          }
          [/code]

          I hope this example helped you understand what's going on! Should you have any questions post!

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by Rajesh V
            In your post, you have said that


            Code: ( text )
            int array[][10];


            won't compile because we need to declare the number of elements.

            But, if i do like this,


            Code: ( text )
            int array[10][];


            The compiler gives the error - declaration of ‘a’ as multidimensiona l array must have bounds for all dimensions except the first
            Yep.
            [code=c]
            int array[10][];
            [/code]
            can't compile becuse you failed to specify the size of the elements.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by Rajesh V
              int array[][10] = { { 0 } };

              It gets compiled successfully.

              Can you please explain what's happening?
              You are using initialization syntax. This is the same as:
              [code=c]
              int array[1][10] = { { 0 } };
              [/code]

              Rememvber:
              [code=c]
              int arr[] = {1,2,3,4,5};
              [/code]

              is the same as:
              [code=c]
              int arr[5] = {1,2,3,4,5};
              [/code]

              Comment

              Working...