2016-10-28 2 views
0

Definieren Sie eine Struktur (Struktur), die drei Elemente enthält, ein Float, ein String und ein kurzes Array von Floats. In derselben Zeile printf das Float, die Zeichenfolge und das erste Element des Arrays mit Leerzeichen dazwischen. Also versuche ich ein Float, eine Zeichenfolge und ein Array von Floats zu drucken. Ich bekomme immer Fehler und ich bin an diesem Punkt nur hirntot. Hier ist mein Code.Zeiger und Druckproblem

#include <stdio.h> 
#include <string.h> 
struct Structure { 
float b; 
char c ; 
float arr[4]; 
}; 

int main (int argc, char *argv[]) 

{ 
int x[4] = {3,5,6,7}; 
int i; 
printf("This is the argument count -> %d\n",argc); 
printf("This is argv[0] %s\n",argv[0]); 
printf("This is argv[1] %s\n",argv[1]); 
for(i = 0; i<=3; i= i + 1) 
    { 
    printf("%d ",x[i]); 
} 
printf("\n"); 
int var = 20; // actual variable declaration 
int *ip;  // pointer variable declaration 

ip = &var; 

printf("Address of var variable: %p\n", &var ); 

printf("Value of *ip variable: %d\n", *ip); 

/*char izard[]="trump"; 
char *ch; 
ch = &izard; 
printf("Address of var variable: %p\n", &izard ); 

printf("Value of *ip variable: %c\n", &izard[4]); 
*/ 
struct Structure structure1; 
structure1.b = 45.4; 
strcpy(structure1.c, "Charizard"); 
strcpy(structure1.arr, "dog"); 
printf("integer : %c/n", structure1.c); 
//printstructure(&structure1); 

return 0; 
} 

/*void printstructure(struct Structure *name){ 
printf("int : %d\n", name->a); 
printf("int : %f\n", name->b); 
printf("int : %c\n", name->c); 


}*/ 
+1

Sie sind fast da. Ein Fehler: Das c-Mitglied sollte Char-Array statt Char sein. Es kann andere Fehler geben, wie das Kopieren eines Strings in ein Float-Array, aber der Compiler ist Ihr Freund und kann Ihnen helfen, sie zu finden. Einige gcc Optionen fast immer verwende ich, sind: gcc -Wall -Wextra -pedantic -std = gnu99 -Wwrite-Strings. Wenn ich im Nitpick-Modus bin, füge ich -Weconversion nur für den Teufel davon hinzu. ;) –

Antwort

0

Sie sollten diese Zeilen überprüfen.

strcpy(structure1.c, "Charizard"); 
strcpy(structure1.arr, "dog") 

c muss ein char-Array sein. Ich verstehe nicht, warum Sie von String zu Float kopieren.

struct Structure { 
    float b; 
    char c[20] ; 
    float arr[4]; 
}; 
+0

Die Funktion strcpy() ist nur für nullterminierte Strings reserviert. Die Verwendung von 'strcpy (structure1.arr," dog ") ist nicht korrekt. –

0

Ihr Problem wurde gelöst. Bitte beachten Sie das unten stehende C-Code-Snippet.

#include <stdio.h> 
#include <string.h> 

struct Structure 
{ 
    float b; 
    char c ; 
    float arr[4]; 
}; 

int main (int argc, char *argv[]) 
{ 
    int x[4] = {3,5,6,7}; 
    int i; 
    printf("This is the argument count -> %d\n",argc); 
    printf("This is argv[0] %s\n",argv[0]); 
    printf("This is argv[1] %s\n",argv[1]); 
    for(i = 0; i<=3; i= i + 1) 
    { 
     printf("%d ",x[i]); 
    } 

    printf("\n"); 
    int var = 20; // actual variable declaration 
    int *ip;  // pointer variable declaration 

    ip = &var; 

    printf("Address of var variable: %p\n", &var ); 

    printf("Value of *ip variable: %d\n", *ip); 

    struct Structure structure1; 
    structure1.b = 45.4; 
    structure1.c = 'A'; 
    structure1.arr[0] = 4.4; /* rest of element required then initalize */ 
    // Error identified Reason: c member of struct which is character type 
    /* strcpy(structure1.c, "Charizard"); */ 
    // Error identified Reason: Use character array instead of float type 
    /* strcpy(structure1.arr, "dog"); */ 

    printf("integer : %c \n", structure1.c); 

    return 0; 
}