Passing an array from a struct as an argument to a function - arrays

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
int votes;
}
candidate;
void array_function(int arr[]);
int main(void)
{
candidate candidates[3];
candidates[0].votes = 5;
candidates[1].votes = 3;
candidates[2].votes= 1;
print_array_function(candidates.votes);
}
void print_array_function(int arr[])
{
for (int i = 0; i < 3; i++)
{
printf("%i\n", arr[i]);
}
}
I'm trying to run this code which declares a struct, feeds values into it and tries to pass an array inside the struct to a function. However, on doing so I get the following error:
test.c:22:30: error: member reference base type 'candidate [3]' is not a structure or union
array_function(candidates.votes);
How do I pass this structs array into the function?

Just declare the function like
void array_function( const candidate arr[], size_t n );
and call like
print_array_function( candidates, 3 );
The function can be defined the following way
void array_function( const candidate arr[], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
printf( "%i\n", arr[i].votes );
}
}

Here, you have an array of structure candidate which each element contains a single int called votes (certainly vote would be a better name for it). Maybe the best approach to do what you want is to create a function print_candidate_array_vote as :
void print_candidate_array_vote(candidate *candidates, unsigned int n_candidates)
{
for (int i = 0; i < n_candidates; i++) {
printf("%d\n", candidates[i].votes);
}
}

Try the code below, have made some changes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct candidate
{
int votes;
}
candidate;
void array_function(int arr[]);
int main(void)
{
candidate candidates[3];
candidates[0].votes = 5;
candidates[1].votes = 3;
candidates[2].votes= 1;
print_array_function(candidates);
}
void print_array_function(candidate arr[])
{
for (int i = 0; i < 3; i++)
{
printf("%i\n", arr[i]);
}
}

Related

undefined reference to ERROR `get_squares'

I have the following simple program I am trying to compile in Dev-C++
main.c
#include <stdio.h>
#include "squares.h"
int main() {
void get_squares(int n, int output[]);
int n = 10;
int squares[10]; // Declare an array large enough to hold 10 integers
get_squares(n, squares); // Use get_squares to populate the array
// Print each element of the array
int i = 0;
for (i = 0; i < 10; i++) {
printf("%d\n", squares[i]);
}
puts("Hello, world!");
return 0;
}
squares.c
#include "squares.h"
void get_squares(int n, int output[]) {
int i = 0;
for (i = 0; i < n; i++) {
// Modifies the array passed as input
output[i] = i * i;
}
}
squares.h
#ifndef SQUARES_H
#define SQUARES_H
void get_squares(int n, int output[]);
#endif
Why do I get the following error? I tried to add void get_squares(int n, int output[]); in the main but it does not work.
hello.c:(.text+0x1e): undefined reference to `get_squares'
collect2.exe: error: ld returned 1 exit status

error: request for member 'length' in something not a structure or union

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct a{
int length;
};
static int a2(int a[]){
int y = 0;
int x = 0;
for (int i=0; i<a.length; i++)
{
if (a[i]%2 == 0)
y += a[i];
else
x += a[i];
}
return x - y;
}
int main()
{
int a[] = {1};
printf("%d\n", a2(a));
return 0;
}
when I run this code I receive the following error "error: request for member 'length' in something, not a structure or union" can anyone help me to understand the error and how to rectify the code? Thanks
The name of structure and the name of variables are not related.
The argument a is a pointer (int a[] in function arguments has the same meaning as int* a) and it doesn't have members.
You have to pass the length of array to pass to functions aside from (the pointer to the first element of) the array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int a2(int a[], int a_length){
int y = 0;
int x = 0;
for (int i=0; i<a_length; i++)
{
if (a[i]%2 == 0)
y += a[i];
else
x += a[i];
}
return x - y;
}
int main()
{
int a[] = {1};
printf("%d\n", a2(a, sizeof(a) / sizeof(*a)));
return 0;
}

How to pass an array of structures to a function by reference in C?

This code is similar to what I am attempting to do, however I am getting errors saying that I am passing incompatable types
#include <stdio.h>
struct numbers{
int num;
};
void fillArray(struct numbers* a[]);
int main(void)
{
struct numbers array[4];
fillArray(&array);
for(int i = 0; i < 4; i++)
{
printf("%d", array[i].num);
}
}
void fillArray(struct numbers* a[])
{
for(int i = 0; i < 4; i++)
{
a[i]->num = i;
}
}
The function parameter
void fillArray(struct numbers* a[]);
is adjusted to
void fillArray(struct numbers ** a);
On the other hand the type of the argument in this call
fillArray(&array);
is struct numbers( * )[4]. The types struct numbers ** and struct numbers ( * )[4] are incompatible.
There is no need to pass a pointer to the array because elements of the array are already passed indirectly if you will pass just the array that is implicitly converted to pointer to its first element.
So what you need is to declare and define the function like
void fillArray( struct numbers a[] )
// or
// void fillArray( struct numbers *a )
{
for(int i = 0; i < 4; i++)
{
a[i].num = i;
}
}
and call it like
fillArray( array );
Take into account that the function depends on magic number 4. It is better to define the function such a way that it could deal with arrays of various numbers of elements.
So I would define the function like
void fillArray( struct numbers a[], size_t n )
// or
// void fillArray( struct numbers *a, size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
a[i].num = i;
}
}
and call it like
fillArray( array, 4 );
Here is demonstrated how the program can look in whole
#include <stdio.h>
struct numbers
{
int num;
};
void fillArray( struct numbers a[], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
a[i].num = i;
}
}
#define N 4
int main(void)
{
struct numbers array[N];
fillArray( array, N );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", array[i].num );
}
putchar( '\n' );
return 0;
}
Its output is
0 1 2 3
To use an array with a different number of elements it is enough to change the value of the macro name N. Thus the program and its function do not depend on the magic number 4.
You probably want this:
#include <stdio.h>
struct numbers {
int num;
};
void fillArray(struct numbers a[]);
int main(void)
{
struct numbers array[4];
fillArray(array);
for (int i = 0; i < 4; i++)
{
printf("%d\n", array[i].num);
}
}
void fillArray(struct numbers a[])
{
for (int i = 0; i < 4; i++)
{
a[i].num = i;
}
}
Your function wants an array of pointers, but you have a single array, so
void fillArray(struct numbers* a);
or
void fillArray(struct numbers a[]);
Moreover using a[i] you are already dereferencing the pointer, so you need . not -> operator.
#include <stdio.h>
struct numbers
{
int num;
};
void fillArray(struct numbers *a);
int main(void)
{
struct numbers array[4];
fillArray(array);
for (int i = 0; i < 4; i++)
{
printf("%d", array[i].num);
}
printf("\n");
}
void fillArray(struct numbers *a)
{
for (int i = 0; i < 4; i++)
{
a[i].num = i;
}
}
Finally using c array decays to pointer to first item of array, so
fillArray(&array);
must be
fillArray(array);
or
fillArray(&array[0]);
Lastly you should pass size of array to your function, instead of using fixed numbers. You can do it using pointer and size
#include <stdio.h>
struct numbers
{
int num;
};
void fillArray(size_t size, struct numbers *a);
int main(void)
{
struct numbers array[4];
fillArray(sizeof(array)/sizeof(array[0]), array);
for (int i = 0; i < 4; i++)
{
printf("%d", array[i].num);
}
printf("\n");
}
void fillArray(size_t size, struct numbers *a)
{
for (size_t i = 0; i < size; i++)
{
a[i].num = i;
}
}
Or using VLAs
#include <stdio.h>
struct numbers
{
int num;
};
void fillArray(size_t size, struct numbers a[size]);
int main(void)
{
struct numbers array[4];
fillArray(sizeof(array)/sizeof(array[0]), array);
for (int i = 0; i < 4; i++)
{
printf("%d", array[i].num);
}
printf("\n");
}
void fillArray(size_t size, struct numbers a[size])
{
for (size_t i = 0; i < size; i++)
{
a[i].num = i;
}
}
It is sufficient to only mention the array name. The compiler will transform it into a pointer to the first element.
So instead of
fillArray(&array);
you write
fillArray(array);
Though this answers your question, of course the function being called must have a compatible definition, which in your case is not so.

C: Printing out the value and memory location of each element of an array using pointers?

I have generated a random array inside the main function, How can I properly print it out using a separate function and inside the function print out the value and memory location of each element of that array using pointers. Here is my code so far:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void printArray(int *pointertoArray, int *Size);
int main (void)
{
srand(time(NULL));
int array[10];
int *pointer = NULL;
for(int i = 0; i < size; i++)
{
array[i] = rand();
*pointer = array[i];
printArray(*pointer,size);
}
}
void printArray(int *pointerToArray, int *size)
{
int i = 0;
do
{
printf("\nValue %d = %p ",i,*pointerToArray);
i++;
}
while(i < size);
}
Here is what I am trying to achieve:
value 1 = 0x7fff0815c0e0
.....
value 10 = 0x7fff0815c0ec
int *size should be int size. You don't pass a pointer, and you don't need a pointer.
Actually, size_t size would be more appropriate.
The call to printArray should be located after the loop. You only want to print the array once.
printArray(*pointer, size); should be printArray(array, size);.
pointerToArray should be named array or pointerToInts.
The value of the element is pointerToArray[i], not i.
The address of the element is pointerToArray+i, not *pointerToArray.
The loop in printArray should be top-tested. (No reason for it to be bottom tested, so play it safe.)
main is declared to return an int, but doesn't.
We get,
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void printArray(int *array, size_t size);
int main() {
srand(time(NULL));
int array[10];
for (int i = 0; i < size; ++i) {
array[i] = rand() % 1000;
}
printArray(array, sizeof(array)/sizeof(array[0]));
return 0;
}
void printArray(int *array, size_t size) {
for (int i = 0; i < size; ++i) {
printf("Value # %p = %d\n", array+i, array[i]);
}
}
Alternative:
void printArray(int *pointerToInt, size_t size) {
for (; size--; ++pointerToInt) {
printf("Value # %p = %d\n", pointerToInt, *pointerToInt);
}
}

Pass by reference in c array

Hi i want to ask about why i need to use printf("\n%d",x); instead of printf("\n%d",*x);?
Thank you very much
#include<stdio.h>
#include<stdlib.h>
#define totalnum 8
void display(int **);
int main()
{
int marks[totalnum]={55,65,75,85,90,78,95,60};
printf("The marks of student A are:");
display(marks);
return 0;
}
void display(int *x)
{
int i;
for(i=0;i<totalnum;i++)
printf("\n%d",x);
}
There is no pass by reference in C. The array decays to a pointer in the display function which you declared wrongly as int ** instead of int * - Compiler should have given you a warning at least about this:
http://ideone.com/R3skNj
This is how your display function should be like:
void display(int *x)
{
int i;
for(i = 0; i < totalnum; i++) {
printf("\n%d",*(x+i)); // or printf("\n%d",x[i]);
}
}
I think your looking for something like this:
#include <stdio.h>
#include <stdlib.h>
#define totalnum 8
void display(int *); //Typ is 'int *' NOT 'int **'
int main() {
int marks[totalnum] = {55,65,75,85,90,78,95,60};
printf("The marks of student A are:");
display(marks);
return 0;
}
void display(int *x) {
int i;
for(i = 0; i < totalnum; i++) {
printf("\n%d",*x); //Prints the value
x++; //increments the pointer
}
}

Resources