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
Related
#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;
}
#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]);
}
}
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);
}
}
I try to implement a function meant to roll a dice a certain amount of time.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int * rollDice(int len) //len = times the dice is rolled.
{
int ints[len];
int i = len-1;
while(i>0)
{
ints[i--] = (rand()%6)+1;
}
return ints;
}
int main(int argc, const char * argv[])
{
int * ints = rollDice(10);
for(int i =0; i<10; i+=1)
{
printf("%d ",*(ints+i));
}
return 0;
}
Program always prints this, is my conception of pointers false ?
104 0 0 0 1919706998 2036950640 1667723631 1836545636 16 48
You cannot do this
return ints;
It's declared on the stack. You need to either pass it in with enough memory or allocated the memory in the function using malloc and pass it back.
int * rollDice(int len) //len = times the dice is rolled.
{
int *ints = malloc(sizeof(int) * len);
int i = len-1;
while(i>0)
{
ints[i--] = (rand()%6)+1;
}
return ints;
}
Harry's answer is right; you can't return the address of a local variable. That variable is destroyed as soon as the function returns.
Instead of having to allocate memory in the function, just pass the array to be filled into the function:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_DICE 10
void rollDice(int *dice, int num_dice)
{
int i;
for (i = 0; i < num_dice; i++) {
dice[i] = (rand() % 6) + 1;
}
}
int main(int argc, const char * argv[])
{
int dice[NUM_DICE];
srand(time()); /* Don't forget this! */
rollDice(&dice, NUM_DICE);
for(int i = 0; i < NUM_DICE; i++)
{
printf("%d ", dice[i]); /* Easier to use brackets than pointer arithmetic. */
}
return 0;
}
I'm having some issues with very simple situations of passing arrays as pointers into functions and returning them. I thought I had pointers figured but I just can't get my head around it.
Here's the code:
int* getLottoDraw();
void printArray(int * array);
int find_matches(int * array1, int * array2);
int main(int argc, char *argv[])
{
int * lotteryDraw = getLottoDraw();
printArray(lotteryDraw);
system("PAUSE");
return 0;
}
int* getLottoDraw(){
int draw[6];
int i;
srand(time(NULL));
for (i = 0; i < 6; i++) {
int r = rand() % 49;
draw[i] = r;
}
return draw;
}
void printArray(int *array){
int i;
for (i = 0; i < 6; i++){
printf("%i ", array[i]);
}
}
One example output is "3 2047 4614546 0 25 45". Not what was hoping for.
You are returning a stack address, which end up being destroyed when the function ends.
Stack variables are local variables, their scope is limited to the function they're created.
They're created on the function, and destroyed when the function ends, so if you've try to access this address later you'll get undefined behavior.
You should have a dynamic allocated pointer to be able to access it outside the function, or return by value, copying the content (which can be costly in an array case).
You could do something like:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int* getLottoDraw();
void printArray(int * array);
int find_matches(int * array1, int * array2);
int main(int argc, char *argv[])
{
int * lotteryDraw = getLottoDraw();
printArray(lotteryDraw);
free(lotteryDraw);
return 0;
}
int* getLottoDraw(){
int* draw = malloc(sizeof(int)*6);
int i;
srand(time(NULL));
for (i = 0; i < 6; i++) {
int r = rand() % 49;
draw[i] = r;
}
return draw;
}
void printArray(int *array){
int i;
for (i = 0; i < 6; i++){
printf("%i ", array[i]);
}
}