[英]Program keeps crashing
I'm new to C, and pointers for that sake, so some help would be wonderful. 我是C的新手,因此是指针,所以有些帮助将是很棒的。 My program has crashed multiple times when I try to run this code. 当我尝试运行此代码时,我的程序多次崩溃。
My method punkt_paa_linje returns an integer, instead of a string, because of pointers I assume. 由于我假定的指针,我的方法punkt_paa_linje返回一个整数而不是字符串。 I have a very vague understanding of pointers, so an explanation would be very appreciated 我对指针的理解很模糊,因此不胜感激。
char punkt_paa_linje(int linje_1[3], int linje_2[3], int punkt[3])
{
int i;
double t1;
double t2;
double t3;
for(i = 0; i < 3; i = i + 1 ){
double t = (punkt[i]-linje_1[i])/linje_2[i];
if(i == 0){
t1 = t;
} else if (i == 1){
t2 = t;
} else if (i == 2){
t3 = t;
}
}
if(t1 == t2 && t2 == t3){
return "true";
} else {
return "false";
}
}
And when I call the function, it returns 36 当我调用该函数时,它返回36
int main()
{
int et[] = {1,2,3};
int to[] = {4,5,6};
int tre[] = {7,8,9};
printf("%d\n", punkt_paa_linje(et, to, tre));
return 0;
}
EDIT: The reason I didn't insert an error message is because there is none 编辑:我没有插入错误消息的原因是因为没有
You should use char *punkt_paa_linje(int linje_1[3], int linje_2[3], int punkt[3])
instead of char punkt_paa_linje(int linje_1[3], int linje_2[3], int punkt[3])
. 您应该使用char *punkt_paa_linje(int linje_1[3], int linje_2[3], int punkt[3])
代替char punkt_paa_linje(int linje_1[3], int linje_2[3], int punkt[3])
。
And use printf("%s\\n", punkt_paa_linje(et, to, tre));
并使用printf("%s\\n", punkt_paa_linje(et, to, tre));
. 。 Then your code will run perfectly and give output true
. 然后,您的代码将完美运行,并输出true
。
otherwise try : 否则尝试:
#include <stdio.h>
int punkt_paa_linje(int linje_1[3], int linje_2[3], int punkt[3])
{
int i;
double t1;
double t2;
double t3;
for(i = 0; i < 3; i = i + 1 ){
double t = (punkt[i]-linje_1[i])/linje_2[i];
if(i == 0){
t1 = t;
} else if (i == 1){
t2 = t;
} else if (i == 2){
t3 = t;
}
}
if(t1 == t2 && t2 == t3){
return 1;
}
else {
return 0;
}
}
int main()
{
int et[] = {1,2,3};
int to[] = {4,5,6};
int tre[] = {7,8,9};
printf("%d\n", punkt_paa_linje(et, to, tre));
return 0;
}
Output : 输出:
1
I will try to explain your mistakes. 我会尽力解释你的错误。
You are trying to return string
literal. 您正在尝试返回string
文字。 Which has type const char*
and its seqention of characters in static storage duration memory, like this 在静态存储持续时间存储器中,其类型为const char*
及其字符序列 ,如下所示
n n+1 n+2 n+3 n+4 <-- Addresses
+---+---+---+---+----+
|'t'|'r'|'u'|'e'|'\0'|
+---+---+---+---+----+
And you are trying to return this string via char
, which is one byte in memory, like this 而且您正在尝试通过char
返回此字符串, char
是内存中的一个字节 ,如下所示
n
+---+
|'t'|
+---+
So you have to return string
instead of char
, where string is passed by pointer to first character in C
. 因此,您必须返回string
而不是char
,其中string通过指针传递到C
第一个字符。
const char * punkt_paa_linje(int linje_1[3], int linje_2[3], int punkt[3])
...
return "true";
%d
specifier expects parameter of type int
, while your function now returns string
, which has specifier %s
. %d
说明符期望使用int
类型的参数,而您的函数现在返回具有说明符%s
string
。
printf("%s\n", punkt_paa_linje(et, to, tre));
Arrays in C
are passed as pointer, so instead of parameters int linje_1[3]
use can simply use int * linje_1
or int linje_1[]
- its same, and it will accept arrays of all lengths. C
中的数组作为指针传递,因此可以使用int * linje_1
或int linje_1[]
代替参数int linje_1[3]
使用-相同,它将接受所有长度的数组。
Here is live demo. 这是现场演示。 Just click run :-) 只需单击运行:-)
You should call your function char *punkt_paa_linje
, cause here it just return a char and you need a str, which is a char *
type. 您应该调用函数char *punkt_paa_linje
,因为这里它只返回一个char,并且您需要一个str,它是char *
类型。
So if you really want your function to return a char
call it char punkt_paa_linje
因此,如果您真的希望函数返回一个char
则将其称为char punkt_paa_linje
There are multiple things when returning string from function in c:- 1.Never return string local variable from function since all local variable of a function will be store in stack and stack will be destroy once function return so local varaible will be invalid now. 从c中的函数返回字符串时有很多事情:1.永远不要从函数返回字符串局部变量,因为函数的所有局部变量都将存储在堆栈中,并且一旦函数返回,堆栈将被破坏,因此局部变量现在将无效。 2.If you want to retrun string function either you should use:- 1.Heap memory. 2.如果要重新运行字符串函数,则应使用:-1.堆内存。 2.static variable. 2.静态变量。 3.String constant literal. 3.字符串常量文字
In your example if you want to return string you should use char * as a return type of a function. 在示例中,如果要返回字符串,则应使用char *作为函数的返回类型。 char * punkt_paa_linje() 字符* punkt_paa_linje()
In place of using %d in printf in your code use %s for printing string. 代替在代码中的printf中使用%d,请使用%s来打印字符串。 printf("%s\\n", punkt_paa_linje(et, to, tre)); printf(“%s \\ n”,punkt_paa_linje(et,to,tre));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.