struct-frage

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • hi leute,

    Meine struct:
    Struktur ArrayInt beinhaltet einmal die Größe eines dynamischen Arrays und den Zeiger auf das dynamische Array. Mit den Funktionen soll die Struktur bearbeitet werden.
    Mein Problem:
    Wenn ich den Speicher mit CreateArray() alloziiere wird die Variable size der Struktur ArrayInt richtig belegt. (Ausgabe: "Größe: 5")
    Wenn ich den alloziierten Speicher mit SetValue() belege, wird die Variable size leider überschrieben und enthält dann den Wert des dritten Elements.
    Kann sich jemand denken warum:

    Mein code:

    Quellcode

    1. /* Includefiles */
    2. #include <stdio.h> /*printf, getchar*/
    3. #include <stdlib.h> /*malloc, realloc*/
    4. /* Declarations of Structures */
    5. struct ArrayInt
    6. {
    7. int size;
    8. int *element;
    9. };
    10. /* Declarations of Functions */
    11. struct ArrayInt *CreateArray(int numberElements);
    12. void DeleteArray(struct ArrayInt *ptr_Array_Int);
    13. int GetValue(struct ArrayInt *ptr_Array_Int, int aElement);
    14. int GetSize(struct ArrayInt *ptr_Array_Int);
    15. void SetValue(struct ArrayInt *ptr_Array_Int, int aElement, int aValue);
    16. void errorOutOfMemory();
    17. void errorWrongNumberOfElements();
    18. /* Main-Function */
    19. int main(int argc, char *argv[])
    20. {
    21. struct ArrayInt *testArray;
    22. testArray = CreateArray(5);
    23. printf("Größe: %d\n", GetSize(testArray));
    24. SetValue(testArray, 0, 22);
    25. SetValue(testArray, 1, 33);
    26. SetValue(testArray, 2, 44);
    27. SetValue(testArray, 3, 55);
    28. SetValue(testArray, 4, 66);
    29. printf("Ausgabe: %d\n", GetValue(testArray, 0));
    30. printf("Ausgabe: %d\n", GetValue(testArray, 1));
    31. printf("Ausgabe: %d\n", GetValue(testArray, 2));
    32. printf("Ausgabe: %d\n", GetValue(testArray, 3));
    33. printf("Ausgabe: %d\n", GetValue(testArray, 4));
    34. printf("Größe: %d\n", GetSize(testArray));
    35. }
    36. /********************************************
    37. CreateArray
    38. ********************************************/
    39. struct ArrayInt *CreateArray(int numberElements)
    40. {
    41. struct ArrayInt *ptr_Array_Int;
    42. if(numberElements <= 0) errorWrongNumberOfElements();
    43. ptr_Array_Int = malloc(sizeof(struct ArrayInt));
    44. if(!ptr_Array_Int) errorOutOfMemory();
    45. ptr_Array_Int->element = malloc(numberElements * sizeof(int));
    46. ptr_Array_Int->size = numberElements;
    47. return(ptr_Array_Int);
    48. }
    49. /********************************************
    50. GetSize
    51. ********************************************/
    52. int GetSize(struct ArrayInt *ptr_Array_Int)
    53. {
    54. return( ptr_Array_Int->size );
    55. }
    56. /********************************************
    57. SetValue
    58. ********************************************/
    59. void SetValue(struct ArrayInt *ptr_Array_Int, int aElement, int aValue)
    60. {
    61. int *ptrTMP;
    62. if(aElement >= ptr_Array_Int->size) return;
    63. //ptrTMP = ptr_Array_Int->element + (aElement * sizeof(int));
    64. //+*ptrTMP = aValue;
    65. *(ptr_Array_Int->element + (aElement * sizeof(int))) = aValue;
    66. //+*ptr_Array_Int->element = aValue;
    67. }
    68. /********************************************
    69. GetValue
    70. ********************************************/
    71. int GetValue(struct ArrayInt *ptr_Array_Int, int aElement)
    72. {
    73. return( *(ptr_Array_Int->element + (aElement * sizeof(int))) );
    74. }
    75. /********************************************
    76. DeleteArray
    77. ********************************************/
    78. void DeleteArray(struct ArrayInt *ptr_Array_Int)
    79. {
    80. if(!ptr_Array_Int) return;
    81. if(ptr_Array_Int->element) free(ptr_Array_Int->element);
    82. }
    83. void errorOutOfMemory()
    84. {
    85. printf("Fatal Error! Out Of Memory.\n");
    86. exit(-1);
    87. }
    88. void errorWrongNumberOfElements()
    89. {
    90. printf("Error! Wrong Parameter: Number Of Elements.\n");
    91. exit(-1);
    92. }
    Alles anzeigen


    Es wäre supertoll, wenn mir jemand einen Hinweis geben könnte.
    Viele Grüße
    Chris
  • Quellcode

    1. void SetValue(struct ArrayInt *ptr_Array_Int, int aElement, int aValue)
    2. {
    3. int *ptrTMP;
    4. if(aElement >= ptr_Array_Int->size) return;
    5. ptr_Array_Int->element[aElement] = aValue;
    6. }
    7. /********************************************
    8. GetValue
    9. ********************************************/
    10. int GetValue(struct ArrayInt *ptr_Array_Int, int aElement)
    11. {
    12. return ptr_Array_Int->element[aElement];
    13. }
    Alles anzeigen


    Gruß void
    "Probleme kann man niemals mit derselben Denkweise lösen,
    durch die sie entstanden sind." (A. Einstein)
  • hi phax & void,

    danke für euren hinweis. hatte fälschlicherweise gedacht, das ich zum zeiger int (für 32-bitter eben 4-byte) addieren muß, um auf das nächste element zu zeigen. und um das hardwareunabhängig zu machen, habe ich eben sizeof(int) addiert.
    o.k. denkfehler.
    is machmal simpler, als man denkt...

    so gehts auch (um zeigersyntax weiter zu nutzen):

    Quellcode

    1. void SetValue(struct ArrayInt *ptr_Array_Int, int aElement, int aValue)
    2. {
    3. ptr_Array_Int->element + aElement = aValue;
    4. }


    gruss
    chris