PASSING POINTER TO A FUNCTION() IN C PROGRAMMING

http://stackoverflow.com/questions/24708783/what-exactly-is-wrong-with-this-code/24709527#24709527

CODE WITH PROBLEM FINDING THE LARGEST VALUE:

 

/*finds the largest value in an array*/
#include <stdio.h>
void find_large(int (*ar), int size, int *ptr1);
int main(void) {
int array[10] = {1, 33, 4, 85, 132, -9, 0, 12, 99, 133};
int size = sizeof(array) / sizeof(*array);
int largest = 0;
int *ptr1 = &largest;
find_large(array, size, &ptr1);
printf(“The largest element is %d.\n”, largest);
}

void find_large(int (*ar), int size, int *ptr1) {
int count, largest = (*ar)[0];
for(count = 0; count < size; count++) {
if(*ar[count] > largest) {
largest = (*ar)[0];
}
}
}

3 replies:

#include <stdio.h>

void find_large(int *ar, int size, int *ptr1);

int main(void) {
int array[10] = {1, 33, 4, 85, 132, -9, 0, 12, 99, 133};
int size = sizeof(array) / sizeof(*array);
int largest = 0;
//int *ptr1 = &largest;//Not required
find_large(array, size, &largest);
printf(“The largest element is %d.\n”, largest);
return 0;
}

void find_large(int *ar, int size, int *ptr1) {
int count, largest = ar[0];
for(count = 1; count < size; count++) {
if(ar[count] > largest) {
largest = ar[count];
}
}
*ptr1 = largest;
}

 

BY RM BEGINNER:

/*finds the largest value in an array*/
#include <stdio.h>

int find_large(int *ar, int size, int *ptr1);

int main(void) {

int array[10] = {1, 33, 4, 85, 132, -9, 0, 12, 99, 133};
int size = sizeof(array) / sizeof(*array);
int largest = 0;
largest = find_large(array, size, &largest);
printf(“The largest element is %d.\n”, largest);
}

int find_large(int *ar, int size, int *ptr1) {
int *p_array;
int count; *ptr1 = *ar;
for(count = 0; count < size; count++) {
if(*ar> *ptr1) {
*ptr1 = *ar;
}
*ar++;
}
return *ptr1;
}

/*finds the largest value in an array*/
#include <stdio.h>

int find_large(int *ar, int size, int *ptr1);

int main(void) {

int array[10] = {1, 33, 4, 85, 132, -9, 0, 12, 99, 133};
int size = sizeof(array) / sizeof(*array);
int largest = 0;
largest = find_large(array, size, &largest);
printf(“The largest element is %d.\n”, largest);
}

int find_large(int *ar, int size, int *ptr1) {
int count; *ptr1 = ar[0];
for(count = 0; count < size; count++) {
if(ar[count] > *ptr1) {
*ptr1 = ar[count];
}
}
return *ptr1;
}

A POINTER WITH & IS AREA OF MEMORY AND IF * IT IS THE VALUE;

🙂

 

 

Advertisements
This entry was posted in C. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s