打印数组中的第一个元素
Printing off the first element in a array
我不确定为什么在尝试查看正在打印的索引 teacher.first 时出现段错误。
我使用 strcpy 将字符串 Adam 放在一个 char 类型的数组中,即在一个结构中。但是我不确定为什么当我试图查看索引 0 处的内容时它会给我一个段错误。
我的假设:
- 当我们没有分配足够的内存时会产生段错误。
-strcpy(teacher.first, "亚当");当在程序开头声明时,将字符串 Adam 放置在给定 32 字节内存的 char/string 数组的索引 0 处。
可能性:
- strcpy(teacher.first, "亚当");将字符串 Adam 单独放入数组中,索引 0 不应为“Adam”,而应为 A.
#include <stdio.h>
#include <string.h>
struct person { /* p e r s o n i s name f o r s t r u c t u r e t y p e */
char first[32]; /* f i r s t f i e l d o f s t r u c t u r e i s a r r a y o f
c h a r */
char last[32]; /* s e c o n d f i e l d i s a r r a y o f c h a r */
int year; /* t h i r d f i e l d i s i n t */
double ppg; /* f o u r t h f i e l d i s d o u b l e */
}; /* e n d i n g ; means end o f s t r u c t u r e t y p e d e f i n i t i o n */
void printperson(struct person personinstance) {
printf("Printing struct person properties : \n");
printf("First and last name : %s %s.\n", personinstance.first,
personinstance.last);
printf("Year:%d.\n", personinstance.year);
printf(" Points per game : %lf .\n", personinstance.ppg);
}
int main(int argc, char* argv[]) {
struct person teacher;
int i;
teacher.year = 2005;
teacher.ppg = 10.4;
strcpy(teacher.first, "Adam");
strcpy(teacher.last, "Hoover");
/*Why'd I get a segment fault. You get segment faults when you're trying to
access memory
that doesn't exist. **/
// Segment fault: --> printf("first element of teacher first is: %s\n",
// teacher.first[0]);
// Whats at the first index of the array?
printperson(teacher);
printf("\n");
printf("first element of teacher first is: %s\n", teacher.first[0]);
}
在评论中,您建议这行代码导致分段错误:
printf("first element of teacher first is: %s\n", teacher.first[0]);
格式说明符 %s
需要 c-string 作为参数,而不是单个字符。要打印单个字符,请使用 %c
.
顺便说一句...
如果您的编译器没有对此报错,请获取更新的编译器以帮助您注意到这些错误。例如,clang-7 报告:
main.c:31:53: warning: format specifies type 'char *' but
the argument has type 'char' [-Wformat]
...of teacher first is: %s\n", teacher.first[0]);
~~ ^~~~~~~~~~~~~~~~
%c
我不确定为什么在尝试查看正在打印的索引 teacher.first 时出现段错误。 我使用 strcpy 将字符串 Adam 放在一个 char 类型的数组中,即在一个结构中。但是我不确定为什么当我试图查看索引 0 处的内容时它会给我一个段错误。
我的假设:
- 当我们没有分配足够的内存时会产生段错误。 -strcpy(teacher.first, "亚当");当在程序开头声明时,将字符串 Adam 放置在给定 32 字节内存的 char/string 数组的索引 0 处。 可能性:
- strcpy(teacher.first, "亚当");将字符串 Adam 单独放入数组中,索引 0 不应为“Adam”,而应为 A.
#include <stdio.h>
#include <string.h>
struct person { /* p e r s o n i s name f o r s t r u c t u r e t y p e */
char first[32]; /* f i r s t f i e l d o f s t r u c t u r e i s a r r a y o f
c h a r */
char last[32]; /* s e c o n d f i e l d i s a r r a y o f c h a r */
int year; /* t h i r d f i e l d i s i n t */
double ppg; /* f o u r t h f i e l d i s d o u b l e */
}; /* e n d i n g ; means end o f s t r u c t u r e t y p e d e f i n i t i o n */
void printperson(struct person personinstance) {
printf("Printing struct person properties : \n");
printf("First and last name : %s %s.\n", personinstance.first,
personinstance.last);
printf("Year:%d.\n", personinstance.year);
printf(" Points per game : %lf .\n", personinstance.ppg);
}
int main(int argc, char* argv[]) {
struct person teacher;
int i;
teacher.year = 2005;
teacher.ppg = 10.4;
strcpy(teacher.first, "Adam");
strcpy(teacher.last, "Hoover");
/*Why'd I get a segment fault. You get segment faults when you're trying to
access memory
that doesn't exist. **/
// Segment fault: --> printf("first element of teacher first is: %s\n",
// teacher.first[0]);
// Whats at the first index of the array?
printperson(teacher);
printf("\n");
printf("first element of teacher first is: %s\n", teacher.first[0]);
}
在评论中,您建议这行代码导致分段错误:
printf("first element of teacher first is: %s\n", teacher.first[0]);
格式说明符 %s
需要 c-string 作为参数,而不是单个字符。要打印单个字符,请使用 %c
.
顺便说一句...
如果您的编译器没有对此报错,请获取更新的编译器以帮助您注意到这些错误。例如,clang-7 报告:
main.c:31:53: warning: format specifies type 'char *' but
the argument has type 'char' [-Wformat]
...of teacher first is: %s\n", teacher.first[0]);
~~ ^~~~~~~~~~~~~~~~
%c