简体   繁体   中英

Program keeps crashing

I'm new to C, and pointers for that sake, so some help would be wonderful. 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. 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

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]) .

And use printf("%s\\n", punkt_paa_linje(et, to, tre)); . Then your code will run perfectly and give output 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. Which has type const char* and its seqention of characters in static storage duration memory, like this

  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

  n
+---+
|'t'|
+---+

So you have to return string instead of char , where string is passed by pointer to first character in 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 .

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.


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.

So if you really want your function to return a char call it 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. 2.If you want to retrun string function either you should use:- 1.Heap memory. 2.static variable. 3.String constant literal.

In your example if you want to return string you should use char * as a return type of a function. char * punkt_paa_linje()

In place of using %d in printf in your code use %s for printing string. printf("%s\\n", punkt_paa_linje(et, to, tre));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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