c - Program keeps crashing -


i'm new c, , pointers sake, wonderful. program has crashed multiple times when try run code.
method punkt_paa_linje returns integer, instead of string, because of pointers assume. have vague understanding of pointers, explanation 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; < 3; = + 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 call function, 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: reason didn't insert error message because there none

i try explain mistakes.

you trying return string literal. has type const char* , seqention of characters in static storage duration memory, this

  n  n+1 n+2 n+3 n+4      <-- addresses +---+---+---+---+----+ |'t'|'r'|'u'|'e'|'\0'| +---+---+---+---+----+ 

and trying return string via char, one byte in memory, this

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

so have return string instead of char, string passed pointer 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 function returns string, has specifier %s.

printf("%s\n", punkt_paa_linje(et, to, tre)); 

arrays in c passed pointer, instead of parameters int linje_1[3] use can use int * linje_1 or int linje_1[] - same, , accept arrays of lengths.


here live demo. click run :-)


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -