c - 无法理解 C 基础知识

标签 c arrays fault

我正在尝试进入 C 编程,但在从 C 数组分配和提取数据(在本例中是与 C 样式字符串之间)时遇到问题。

请指出您在这里看到的任何错误。

我主要是一名 C++/Python 程序员,因此请尽可能简单地解释内存使用和管理。

#include <stdio.h>

typedef struct AuthorInfo {
  char* firstName;
  char* lastName;
} AuthorInfo;

typedef struct BookEntry {
  char bookID;
  char* bookName;
  AuthorInfo author;
} BookEntry;

void assign_str(const char** from, char** to) {
  int size = sizeof(from)/sizeof(char);
  printf((char)size);
  printf('\n');
  for (int i=0; i < size; i++) {
    (*to)[i] = (*from)[i];
  };
};

BookEntry BookEntry_(const int id, const char* bName, const char* aF, const char*aL, BookEntry* ret) {
  ret->bookID = id;
  ret->bookName = (char*)malloc(sizeof(bName));
  ret->author.firstName = (char*)malloc(sizeof(aF));
  ret->author.lastName = (char*)malloc(sizeof(aL));
  assign_str(bName, &ret->bookName);
  assign_str(aF, &ret->author.firstName);
  assign_str(aL, &ret->author.lastName);
}

void display_book(BookEntry* entry) {
  printf(entry->bookName);
  printf('\n');
  printf(entry->author.firstName);
  printf(' ');
  printf(entry->author.lastName);
  printf('\n');
};

int main(int argc, char** args) {
  BookEntry book;
  book.bookID = 0;
  assign_str("Tom Sawyer", &book.bookName);
  assign_str("Mark", &book.author.firstName);
  assign_str("Twain", &book.author.lastName);
  display_book(&book);
  return 0;
};

使用 gcc goof.c -o goof -std=c11 编译此代码会产生以下结果:

goof.c: In function ‘assign_str’:
goof.c:16:10: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
   printf((char)size);
          ^
In file included from goof.c:1:0:
/usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but argument is of type ‘char’
 extern int printf (const char *__restrict __format, ...);
            ^
goof.c:16:3: warning: format not a string literal and no format arguments [-Wformat-security]
   printf((char)size);
   ^
goof.c:17:10: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
   printf('\n');
          ^
In file included from goof.c:1:0:
/usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but argument is of type ‘int’
 extern int printf (const char *__restrict __format, ...);
            ^
goof.c:17:3: warning: format not a string literal and no format arguments [-Wformat-security]
   printf('\n');
   ^
goof.c: In function ‘BookEntry_’:
goof.c:25:26: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
   ret->bookName = (char*)malloc(sizeof(bName));
                          ^
goof.c:25:26: warning: incompatible implicit declaration of built-in function ‘malloc’
goof.c:25:26: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
goof.c:28:14: warning: passing argument 1 of ‘assign_str’ from incompatible pointer type [-Wincompatible-pointer-types]
   assign_str(bName, &ret->bookName);
              ^
goof.c:14:6: note: expected ‘const char **’ but argument is of type ‘const char *’
 void assign_str(const char** from, char** to) {
      ^
goof.c:29:14: warning: passing argument 1 of ‘assign_str’ from incompatible pointer type [-Wincompatible-pointer-types]
   assign_str(aF, &ret->author.firstName);
              ^
goof.c:14:6: note: expected ‘const char **’ but argument is of type ‘const char *’
 void assign_str(const char** from, char** to) {
      ^
goof.c:30:14: warning: passing argument 1 of ‘assign_str’ from incompatible pointer type [-Wincompatible-pointer-types]
   assign_str(aL, &ret->author.lastName);
              ^
goof.c:14:6: note: expected ‘const char **’ but argument is of type ‘const char *’
 void assign_str(const char** from, char** to) {
      ^
goof.c: In function ‘display_book’:
goof.c:34:3: warning: format not a string literal and no format arguments [-Wformat-security]
   printf(entry->bookName);
   ^
goof.c:35:10: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
   printf('\n');
          ^
In file included from goof.c:1:0:
/usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but argument is of type ‘int’
 extern int printf (const char *__restrict __format, ...);
            ^
goof.c:35:3: warning: format not a string literal and no format arguments [-Wformat-security]
   printf('\n');
   ^
goof.c:36:3: warning: format not a string literal and no format arguments [-Wformat-security]
   printf(entry->author.firstName);
   ^
goof.c:37:10: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
   printf(' ');
          ^
In file included from goof.c:1:0:
/usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but argument is of type ‘int’
 extern int printf (const char *__restrict __format, ...);
            ^
goof.c:37:3: warning: format not a string literal and no format arguments [-Wformat-security]
   printf(' ');
   ^
goof.c:38:3: warning: format not a string literal and no format arguments [-Wformat-security]
   printf(entry->author.lastName);
   ^
goof.c:39:10: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
   printf('\n');
          ^
In file included from goof.c:1:0:
/usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but argument is of type ‘int’
 extern int printf (const char *__restrict __format, ...);
            ^
goof.c:39:3: warning: format not a string literal and no format arguments [-Wformat-security]
   printf('\n');
   ^
goof.c: In function ‘main’:
goof.c:45:14: warning: passing argument 1 of ‘assign_str’ from incompatible pointer type [-Wincompatible-pointer-types]
   assign_str("Tom Sawyer", &book.bookName);
              ^
goof.c:14:6: note: expected ‘const char **’ but argument is of type ‘char *’
 void assign_str(const char** from, char** to) {
      ^
goof.c:46:14: warning: passing argument 1 of ‘assign_str’ from incompatible pointer type [-Wincompatible-pointer-types]
   assign_str("Mark", &book.author.firstName);
              ^
goof.c:14:6: note: expected ‘const char **’ but argument is of type ‘char *’
 void assign_str(const char** from, char** to) {
      ^
goof.c:47:14: warning: passing argument 1 of ‘assign_str’ from incompatible pointer type [-Wincompatible-pointer-types]
   assign_str("Twain", &book.author.lastName);
              ^
goof.c:14:6: note: expected ‘const char **’ but argument is of type ‘char *’
 void assign_str(const char** from, char** to) {
      ^

运行代码会导致 bash 显示:

Segmentation fault (core dumped)

最佳答案

那里有很多错误,没有数组,只有结构。

  • 首先,您必须包含 stdlib 库 ( #include <stdlib.h> )
  • 其次,printf函数不能这样使用。 该函数需要一个字符串来知道如何打印数据,例如:printf("an int: %d",myInt);printf("a string: %s",myString); 。请注意 %d 或 %s,它们指示放置数据的位置。
  • 第三,我想你想要这个 void assign_str(const char* from, char** to)

关于c - 无法理解 C 基础知识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37379625/

相关文章:

c - 为什么汇编中 if 语句中的条件是逻辑“NOT”?

ruby - 除了数组值之外,如何使用散列?或者如何创建哈希线?

c - 读入字符串的 fscanf 问题

php - Foreach 不工作 - PHP

Javascript Foreach 的日期格式打印相同的日期

c - 与名为 Fill-Zone 的游戏相关的 C 语言段错误

c# - 如何处理 WCF 故障异常

python - 如何从 python 代码中引发 Suds.WebFault?

c - 找出 GCC 包含路径是什么

c - 使用 mmap 映射的内存,并与 mprotect 一起使用