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));
Related
I'm trying to count the number of indexes of an undefined char array which is used as a parameter in the function.
I am already aware that if my array was fixed I can use "sizeof", which isn't the case here.
Attempt:
int counting(char *name3) {
int count = 0;
int i;
//I have no idea what to put as my condition nor do I believe
//I am approaching this situation correctly...
for (i = 0; i < sizeof(name3); i++) {
if (name3[i] != '\0') {
count++;
}
}
return count;
}
Then if it is run by the following code
int main(void) {
char *name = "Lovely";
int x = counting(name);
printf ("The value of x = %d", x);
Prints: The value of x = 0
Any help or pointers would be amazing. Thank you in advance.
In C, Every string ends with '\0' (Null Character)
You can iterate until you meet the Null Character
The example code would be like this
char* name = "Some Name";
int len = 0;
while (name[len] != '\0') {
len++;
}
Also, if it is a char pointer, not char array, sizeof(char*) will always return 4 in 32-bit application and return 8 in 64-bit application (the size of the 'pointer' itself - the memory address size)
#include <stdio.h>
int main()
{
int i=0;
char *name = "pritesh";
for(i=0;;i++)
{
if(name[i] == '\0')
{
break;
}
}
printf("%d", i);
return 0;
}
This should work
note: this might be syntactically incorrect as I have not had my hands on c since a long time
I am scanning for values using function .
int **array(int * counter) {
int **vrat;
int max = 5;
int index = 0;
int i;
vrat = malloc(max*sizeof(int*));
for ( i = 0; i < max ; i++) {
vrat[i] = malloc(2 * sizeof(int));
}
int x;
int y;
char c;
while (scanf("%d%c%d", &x, &c, &y) != EOF) {
vrat[index][0] = x;
vrat[index][1] = y;
index++;
}
*counter = index;
return vrat;
}
and calling it in main to return the array, which works .
int main()
{
int counter=0;
int **mej;
int gg;
mej = array(&counter);
int i;
gg = pocet(mej, &counter);
return 0;
}
what is bothering my mnd is "pocet" function , i am passing an array in it and want to print its value . but it always print undefined numbers
Function looks like this
int pocet(int array[][2],int *counter) {
int poc = 0;
int i;
for ( i =0; i < *counter ;i++) {
printf("cislo = %d", array[i][0]);
}
return poc;
}
as you can see , it has static 2nd dimension , how can i make this work?
You are allocating space for 5 pointers, you should multiply by the size of a pointer and not the size of an int, like this
vrat = malloc(max * sizeof(int *));
/* ^ pointer */
Although you can completely avoid the mistake by multiplying by the size of the pointer type like this
vrat = malloc(max * sizeof(*vrat));
and ALWAYS check that malloc() has not returned NULL before actually using the pointer.
Also, don't compare scanf() to EOF since it's very unlikely to get an EOF before an input error because it requires explicit input from the user, instead do this
while(scanf("%d%c%d", &x, &c, &y) == 3)
The return type of your function should match that of the returned object, in this case int **, change the function signature to
int **array(int *content)
vrat = malloc(max*sizeof(int*));
How do you know whether the memory successfully got allocated or not?
Always check the pointer returned by malloc & family functions for equivalence with NULL to avoid possible SegFault.
while (scanf("%d%c%d", &x, &c, &y) != EOF)
What if there's matching failure for c? Then scanf will return 1 and while will still go for iteration even if scanf could not store values in c and y.
So always compare scanf return value with no. of arguments instead of EOF
while (scanf("%d%c%d", &x, &c, &y) != EOF) {
vrat[index][0] = x;
vrat[index][1] = y;
index++;
}
Here what if index >= max?
Since you have allocated memory for only max integer pointers you must not try to access the memory beyond the allocated chunk.
Solution: Change while condition to
while ((scanf("%d%c%d", &x, &c, &y) == 3) && (index < max))
So the correct code snippet would be:
vrat = malloc(max*sizeof(int*));
if(!vrat)
{
printf("vrat: malloc failed!\n");
exit(1);
}
for ( i = 0; i < max ; i++) {
vrat[i] = malloc(2 * sizeof(int));
if(!vrat[i])
{
printf("vrat[%d]: malloc failed!\n", i);
exit(1);
}
}
int x;
int y;
char c;
while ((scanf("%d%c%d", &x, &c, &y) == 3) && (index < max)) {
vrat[index][0] = x;
vrat[index][1] = y;
index++;
}
An int** is not an int[][2]. The first is a pointer to pointers to int while the second (as an argument) is a pointer to int[2]. You cannot convert between the two the way you tried. You should have gotten a compiler warning/error about this.
The easiest way to fix this is to have pocet take an int** as the first argument. Fix the issues others have mentioned as well.
You have defined int **vrat;
When you do malloc, you need to use int ** or int * and not int
I'm a java student who's currently learning about pointers and C.
I tried to make a simple palindrome tester in C using a single array and pointer arithmetic.
I got it to work without a loop (example for an array of size 10 :*(test) == *(test+9) was true.
Having trouble with my loop. School me!
#include<stdio.h>
//function declaration
//int palindrome(int *test);
int main()
{
int output;
int numArray[10] = {0,2,3,4,1,1,4,3,2,0};
int *ptr;
ptr = &numArray[0];
output = palindrome(ptr);
printf("%d", output);
}
//function determine if string is a palindrome
int palindrome(int *test) {
int i;
for (i = 0; i <= (sizeof(test) / 2); i++) {
if (*(test + i) == *(test + (sizeof(test) - i)))
return 1;
else
return 0;
}
}
The Name of the array will itself acts as a pointer to an first element of the array, if you loose the pointer then there is no means for you to access the element of the array and hence you can send just the name of the array as a parameter to the function.
In the palindrome function:
you have used sizeof(test)/2. what happens is the address gets divided which is meaningless and hence you should not use that to calculate the mid element.
sizeof the pointer will be the same irrespective of the type of address that gets stored.
Why do you copy your pointer in another variable?
int *ptr;
ptr = &numArray[0];
Just send it to you function:
palindrome(numArray);
And sizeof(test) give you the memory size of a pointer, it's not what you want. You have to give the size in parameter of your function.
int palindrome(int *test, int size){
...
}
Finally your code must look like this:
#include<stdio.h>
int palindrome(int *test, int size);
int main()
{
int output;
int numArray[10] = {0,2,3,4,1,1,4,3,2,0};
output = palindrome(numArray, 10);
printf("%d", output);
}
//function determine if string is a palindrome
int palindrome(int *test, int size) {
int i;
for (i = 0; i < size / 2; i++) {
if (*(test + i) != *(test + (size - 1) - i))
return 0;
}
return 1;
}
I am new to C and pointers and I'd like know if its possible to pass an array pointer to a function instead of passing the array of characters itself. I am posting the snippet from the code.
char ipAddress[24];
int i, j;
for (i = 12; i <= 13; i++)
{
for (j = 1; j <= 254; j++)
{
sprintf(ipAddress,"192.168.%d.%d",i,j);
runCommand(ipAddress);
}
}
// ...
int runCommand (char x[24])
{
// Do stuff.
}
Arrays are always passed by pointer in C, not passed by value (copyed)
So
int runCommand (char x[24]);
is close equivalent of
int runCommand (char *x);
Yes, it is possible to pass a pointer to an array to a function. No, it is probably not what you want.
int runCommand(char (*x)[24])
{
if ((*x)[0] == '\0') // Option 1
return -1;
if (x[0][0] == '\0') // Option 2: equivalent to option 1.
return -1;
...
}
void alternative(void)
{
char y[24] = "Samizdat";
printf("%d\n", runCommand(&y));
}
That says x is a pointer to an array of 24 characters. Be very careful, though. In general, you do not want to pass a pointer to an array; you just want to pass pointers around.
int runCommand(char x[24]) // Or: char *x
{
if (x[0] == '\0') // Option 1
return -1;
...
}
void alternative(void)
{
char y[24] = "Samizdat";
printf("%d\n", runCommand(y));
}
In C programming, how do i check whether char array[] contains int, float or double value and also store the value using respective data type?
Example:
If the char array[] contains 100 - its int value and should be store in int a.
if the char array contains 10000.01 its float value and should be stored in float b.
The only way you can store mixed types in an array is to have an array of pointers.
You'd need to use a struct or a union to store each one too like so:
#define TYPE_INT 1
#define TYPE_FLOAT 2
#define TYPE_STRING 3
typedef struct {
int type;
void *ptr;
} object;
object* mkobject( int type, void * data ){
object * obj = (object*)malloc(COUNT*sizeof(object))
obj->type = type;
obj->ptr = data;
return obj;
}
No using the above you can store type information
void * intdup( int original ) {
int * copy = (int*) malloc(1*sizeof(int));
*copy = original;
return (void*) copy;
}
void * floatdup( float original ) {
float * copy = (float*) malloc(1*sizeof(float));
*copy = original;
return (void*) copy;
}
int COUNT = 3;
objects** objectlist = (object**)malloc(COUNT*sizeof(object*))
// -- add things to the list
int a_number = 2243;
float a_float = 1.24;
char* a_string = "hello world";
objectlist[0] = mkobject( TYPE_STRING, strdup(a_string) );
objectlist[1] = mkobject( TYPE_INT, intdup(a_number) );
objectlist[2] = mkobject( TYPE_FLOAT, intdup(a_float) );
// iterate through a list
for ( int x = 0; x < COUNT; x++ ){
switch( objectlist[x]->type ){
case TYPE_STRING:
printf("string [%s]\n",(char*) objectlist[x]->ptr );
break;
case TYPE_FLOAT:
printf("float [%f]\n", *(float*) objectlist[x]->ptr );
break;
case TYPE_INT:
printf("int [%d]\n", *(int*) objectlist[x]->ptr );
break;
default;
printf("unintialized object\n");
break;
}
}
In C, unlike in other languages, you have to define the type of the variable at compile time. So if you have a char variable (or char array), you have char and not int and not float and not double.
Since it is not possible to define variables types at run-time, you would still need to have variables of the correct data type defined at compile time. If that is not a problem, you could probably scan the string and look for decimal separators to determine if it is a float or integer value. But perhaps not the most robust method :-)
I think you want to use a union (from my understanding of your answer).
union {
char a[4];
float b;
int c;
} dude;
// ...
union dude woah;
woah.a = "abc";
puts(a);
woah.b = 4.3;
printf("%f\n", woah.b);
woah.c = 456;
printf("%d\n", woah.c);
If you are storing a value like:
"100.04"
in the char array or something like that you can do this to check if the number has a decimal or not:
double check = atof(theChar);
if (check % 1 > 0) {
//It's a real number
}
else {
//It's more specifically an integer
}
If that is what you mean. Your question is a little unclear to me.
Although, this isn't really type checking, it is just testing where the thing has a decimal or not... Like others have said you can't do it because the char* is defined during compilation not during run time and can't be change.
Assume the number is a floating point number and use strtod().
If the conversion worked, the number can be integer. Check limits and closeness to a proper integer and convert again if ok.
Pseudo-code
char *input = "42.24";
char *err;
errno = 0;
double x = strtod(input, &err);
if (errno == 0) {
/* it's (probably) a floating point value */
if ((INT_MIN <= x) && (x <= INT_MAX)) {
if (fabs(x - (int)x) < 0.00000001) {
/* It's an integer */
int i = x;
}
}
} else {
/* deal with error */
}