简体   繁体   English

打印出数组中的第一个元素

[英]Printing off the first element in a array

I'm not sure why I get a segment fault when I try to see what index teacher.first is printing.我不知道为什么当我尝试查看什么 index Teacher.first 正在打印时会出现段错误。 I used strcpy to place the string Adam in an array of type char, thats in a struct.我使用 strcpy 将字符串 Adam 放入一个 char 类型的数组中,即结构体中。 But i'm not sure why its giving me a segment fault when I try to see what's at index 0.但是当我尝试查看索引 0 处的内容时,我不确定为什么它会给我一个段错误。

My assumptions:我的假设:

  • Segment Fault accrue when we haven't allocated enough memory.当我们没有分配足够的内存时会产生段错误。 -strcpy(teacher.first, "Adam"); -strcpy(teacher.first, "Adam"); places the string Adam at index 0 of the char/string array that given 32 bytes of memory, when declared at the beginning of the program.当在程序开始时声明时,将字符串 Adam 放置在给出 32 字节内存的字符/字符串数组的索引 0 处。 Possibilities:可能性:
  • strcpy(teacher.first, "Adam"); strcpy(老师。第一,“亚当”); places the string Adam separately into the array and index 0 should not be "Adam" but should be A.将字符串 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]);
}

In the comments, you suggest that this line of code is causing the segmentation fault:在评论中,您建议这行代码导致分段错误:

printf("first element of teacher first is: %s\n", teacher.first[0]);

The format specifier %s expects a c-string as the argument, not a single character.格式说明符%s需要一个 c 字符串作为参数,而不是单个字符。 To print a single character use %c .要打印单个字符,请使用%c

As an aside...作为旁白...

If your compiler is not complaining about this, please get a newer compiler to help you notice these errors.如果您的编译器没有对此抱怨,请获取更新的编译器以帮助您注意这些错误。 For example, clang-7 reports:例如,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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM