Pointer of int

Posts 1–10 of 10 · Page 1 of 1
Pointer of int
I'm having a stroke or some shit but i cant seem to get this working (it been a while since i deal with pointers).
I have a function that takes in a int pointer
Code:
void getINPUTandClear(int * something)
I declared my var
Code:
int day = -1;
\\or
int * day;
*day= -1;
and I'm trying to pass the point to the function by
Code:
getINPUTandClear(&day);
\\or
getINPUTandClear(*day);
\\or 
getINPUTandClear(day);
but all of those wouldn;t work. What's am i doing wrong?
idk much about c++ pointers but shouldn't it be:
void getINPUTandClear(int & something)
Use references wherever possible, use pointers only if you can't avoid them.

Regardless, if you want to use pointers, this should work just fine:

Code:
void getINPUTandClear(int* something);

int day = -1;

getINPUTandClear(&day);
or else if you declare day as a pointer:

Code:
void getINPUTandClear(int* something);

int* day;
*day = -1;

getINPUTandClear(day);
Quote Originally Posted by WasserEsser View Post

or else if you declare day as a pointer:

Code:
void getINPUTandClear(int* something);

int* day;
*day = -1;

getINPUTandClear(day);
In both this and OPs example the day pointer is uninitialized.
Quote Originally Posted by Hell_Demon View Post
In both this and OPs example the day pointer is uninitialized.
Correct, overlooked that.
Quote Originally Posted by WasserEsser View Post
Use references wherever possible, use pointers only if you can't avoid them.

Regardless, if you want to use pointers, this should work just fine:

Code:
void getINPUTandClear(int* something);

int day = -1;

getINPUTandClear(&day);
or else if you declare day as a pointer:

Code:
void getINPUTandClear(int* something);

int* day;
*day = -1;

getINPUTandClear(day);
The day pointer will need to be initialized to point to the variable that will be actually modified.
Quote Originally Posted by Leave_Em_Leakin View Post
The day pointer will need to be initialized to point to the variable that will be actually modified.
Read the rest of the thread next time.
Quote Originally Posted by WasserEsser View Post
Read the rest of the thread next time.
I must have overlooked that.
In C:

(U should use prints to understand how things work.)

Code:
#include <stdio.h>

double minmaxavg(double array[], int num, double *max,double *min);

double minmaxavg(double array[], int num, double *max,double *min){
	int i;
	double sum =0;
	for(i =0;i< num;i++){
		if(array[i] > *max){
			*max = array[i];
		}
		if(array[i] < *min){
			*min = array[i];
		}
		sum +=array[i];
	}
	return sum;
}

void main(){
	double sum;
	double max =1;
	double min =1;
	double arr[] = {1,3,2,5,4,6,8,7,99,0};
	//double *pArr;
	double *pMax;
	double *pMin;
	//pArr = &arr;
	pMax = &max;
	pMin = &min;
	sum =minmaxavg(arr,10,pMax,pMin);
	printf("sum:%g min:%g max:%g",sum,min,max);
}
youtube com/watch?v=Fa6S8Pz924k

glhf
Posts 1–10 of 10 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us