指针和打印问题

时间:2016-10-28 05:15:12

标签: c

定义一个包含三个成员的结构(结构),一个浮点数,一个字符串和一个短浮点数组。在同一行上,printf float,string和数组的第一个元素,中间有空格。 所以我试图打印一个浮点数,一个字符串和一个浮点数组。我一直在犯错误,此时我只是脑子里死了。这是我的代码。

#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);


}*/

2 个答案:

答案 0 :(得分:0)

你应该检查这些行。

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

c必须是char数组。我不明白你为什么要从字符串复制到浮点数。

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

答案 1 :(得分:0)

您的问题已解决。请参阅下面的C代码段。

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