【发布时间】:2022-01-21 23:23:14
【问题描述】:
我在尝试编译时遇到这些错误,唯一加下划线的是 strcpy_s(name, strlen(input)+1, input);这是警告。这是作业的一部分,部分说明是制作 Topic.c(下面的代码)和 Topic.h,但没有说明在 .h 文件中放入什么内容。我以前从未见过这些错误并对其进行了研究,但我找不到与此问题相关的参考资料。这不是一个修复代码分配,它应该可以运行我只应该理解这些概念。不知道要问什么问题。我检查了几次以确保代码输入正确。
函数“int __cdecl invoke_main(void)”(?invoke_main@@YAHXZ) 中引用的 LNK2019 未解析的外部符号 main
和
LNK1120 1 个未解决的外部问题
和 1 个警告
C6387 'name' 可能是 '0':这不符合函数 'strcpy_s' 的规范。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Swap 1 method with scalars: why does this not work?
*/
void swap1(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
/*
* swap 2 method with pointers: why does this work?
*/
void swap2(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
/*
* Play Pointer method: Don't forget to clean up your malloc!
*/
char* playPointer(char* input)
{
// Allocate memory to hold a copy of the input array, copy input to it, and
return a pointer to a new array
// What is the advantage of assigning address of a variable to a pointer
variable?
// Why does swap1() not work, and why does swap2() work?
// How does strpy_s() make code more secure?
// How does strpy_s() demonstrate defensive coding?
// Look up strncpy() and compare it to strpy_s() How are they similar?
char* name = malloc(strlen(input)+1);
strcpy_s(name, strlen(input)+1, input);
return name;
}
【问题讨论】:
-
您缺少
main()函数。如果这不是主程序,则在编译时使用-c选项只是创建一个.o文件,而不是尝试链接它。 -
警告是因为你没有检查
malloc()是否成功。 -
为什么代码前后都有
'''?如果你想标记一个代码块,它应该是 3 个反引号,而不是引号,它们应该在代码之前的行上。但既然你已经缩进了代码,你就不需要那个标记了。 -
不确定如何检查 malloc 是否成功。尝试添加 main 我需要 int main() 吗?感谢您提交代码的提示
-
if (name != NULL)