I need to know if i want to make an array that every element of the array is a pointer to a linked list and pass the array to a function, the function is void because I need to change the array
typedef struct n{
char *S;
int num;
}list;
int (main){
list *Array[50];
return 0;
}
should the function be void changeArray(list A[]); or void changeArray(list *A[]); or void changeArray(list **A[]);
The function could be either void changeArray(list *A[]) or void changeArray(list **A). Both signatures would accept an array of pointers, and let you change elements of that array:
void changeArray(list *A[]) {
...
A[0] = malloc(list);
}
The array is defined like
list *Array[50];
So if you want to pass this array to a function then it should be declared like
void changeArray( list *Array[50], size_t n );
or
void changeArray( list *Array[], size_t n );
or
void changeArray( list **Array, size_t n );
and called like
changeArray( Array, 50 );
In any case a declaration of an array as a function parameter is adjusted to a pointer to object of the type of elements of the array.
Related
I am trying to pass pointer to an array of structures to another function
#include<stdio.h>
#include<string.h>
typedef struct CovidData{
char region[7];
char towns[12];
char race[12];
int yearlyIncome;
int members;
int testedMembers;
int testedPositive;
} CovidData;
void RandomDataGenerator(CovidData *data[] ,int count)
{
for(int i=0;i<count;i++){
memcpy(data[i]->region,"david",sizeof("david"));
memcpy(data[i]->towns,"david",sizeof("david"));
memcpy(data[i]->race,"david",sizeof("david"));
data[i]->yearlyIncome=1000;
data[i]->members=99;
data[i]->testedMembers=88;
data[i]->testedPositive=656;
}
}
int main() {
struct CovidData data[100];
RandomDataGenerator(&data,2);
for(int i=0;i<5;i++){
printf("%s",data[i].region);
}
}
But it throws an error while compiling in terminal with gcc
incompatible pointer types passing 'CovidData (*)[100]' to
parameter of type 'CovidData
CovidData *data[] is grouped as CovidData *(data[]), so it declares an array of pointers to CovidData. For a pointer to an array, you would use CovidData (*data)[].
However, we rarely pass a pointer to an array. Usually, it is sufficient and convenient merely to pass a pointer to the first element. Thus, you would declare the parameter as CovidData *data and pass it as RandomDataGenerator(data, 2).
If you did declare the parameter as a pointer to an array, you would not use it with data[i]->region. You would need *data to get the array before applying the subscript, and again you would need parentheses for correct grouping: (*data)[i]->region.
Change your function to void RandomDataGenerator(CovidData *data ,int count); and pass only the pointer to the first array element, like this RandomDataGenerator(data,2);
#include<stdio.h>
#include<string.h>
typedef struct CovidData{
char region[7];
char towns[12];
char race[12];
int yearlyIncome;
int members;
int testedMembers;
int testedPositive;
} CovidData;
void RandomDataGenerator(CovidData *data ,int count)
{
for(int i=0;i<count;i++){
memcpy(data[i].region,"david",sizeof("david"));
memcpy(data[i].towns,"david",sizeof("david"));
memcpy(data[i].race,"david",sizeof("david"));
(data+i)->yearlyIncome=1000;
data[i].members=99;
data[i].testedMembers=88;
data[i].testedPositive=656;
}
}
int main() {
struct CovidData data[100];
RandomDataGenerator(data,2);
for(int i=0;i<5;i++){
printf("%s\n",data[i].region);
}
}
I have the following structs. A tDiscountsShop contains, let's say 5 tDiscount structs.
#define MAXDISCOUNTS 50
typedef enum {FALSE, TRUE} bool;
typedef struct {
int dni;
float discount;
bool changed;
} tDiscount;
typedef struct {
tDiscount discounts[MAXDISCOUNTS];
int numDiscounts;
} tDiscountsShop;
I would like to sort by dni using qsort. I'm trying using the following code:
int compare(const void *s1, const void *s2)
{
tDiscount *e1 = (tDiscount *)s1;
tDiscount *e2 = (tDiscount *)s2;
return e1->dni - e2->dni;
}
qsort (discountsShop->discounts, discountsShop->numDiscounts, sizeof(discountsShop->discounts), compare);
I'll appreciate if I could have an explanation of what I'm doing wrong and how I could solve this issue. Thanks in advance.
This
sizeof(discountsShop->discounts)
gives you the size of the whole array.
What you need/want is the size of one element.
To get this do
sizeof(*discountsShop->discounts)
or
sizeof(discountsShop->discounts[0])
From qsort()'s documentation:
void qsort(void *base, size_t nel, size_t width,
int (*compar)(const void *, const void *));
[...]
The size of each object, in bytes, is specified by the width argument.
You are passing the size of the whole array instead of the size of each element.
sizeof(discountsShop->discounts)
should be
sizeof(discountsShop->discounts[0])
I'm trying to print out all the members of each structure from a list. I was provided the print functions below to print out an element from a generic list.
Here is the structure definition of my list, which is in a generic list ADT c file:
struct list_type {
void *data;
int size;
int capacity;
int elementSize;
int (*comparePtr) (void *d1, void *d2);
};
So in a generic list ADT c file, I have this print function:
// client needs to send a pointer to a function capable of printing an element
void printl(ListType listptr, void (*printItem) (void *d)) {
int i;
for(i = 0; i < listptr->size; i++) {
// since we cannot use data[i], we need to calculate the address
// of the element to be sent to the client's print function
// since data is of type void, the formula is:
// the beginning address of the array + (offset x element size)
printItem(listptr->data + i * (listptr->elementSize) );
printf(" ");
}
printf("\n");
}
I call my printl function like so:
printl(clientList, printItem);
In my driver file, there's a function to print out an element from the list:
void printItem (int* p) {
printf("%d", *p);
//`my attempt at printing the members of an individual structure from the list
// printf("%s", ((Client *)&p)[0]);
}
Here is my Client structure definition:
struct client_tag {
char id[5];
char name[30];
char email[30];
char phoneNum[15];
};
When I run the program, I get a bunch of weird characters. How do I fix this?
Assuming you left out the line
typedef struct client_tag Client;
and assuming the strings of Client are guaranteed to be null terminated, this is the idea:
void printItem (const Client* p) {
printf("%s\n", p->id);
}
This is the data type that I declared:
struct Element{
char name[21], symbol[4];
double atomicMass;
int valence;
};
typedef struct Element myElements;
myElements data[20];
If I just want to pass the name members of
data[20]
to qsort(), how to do that?
Not sure if this would be the correct way to pass to the function:
qsort(data->name, 20, sizeof(myElements), compare);
You don't pass just member. Right way is to write a helper compare function which compares 2 elements by their name and use it.
static int
cmpElement(const void *p1, const void *p2)
{
return strcmp(((const Element *) p1)->name, ((const Element *) p2)->name);
}
qsort(data, 20, sizeof data[0], cmpElement);
How should I parse an array of structures as parameter to a function?
For example, I have the following structure definition:
struct Town
{
char *TownName;
char **GiftList;
int *GiftCount;
int GiftTypes;
};
and a declaration of an array of such structures, in my main:
struct Town TownList[100];
struct Town AuxiliaryStructure;
I have written a custom sorting function for this array, in which I want to make use of each structure's fields, but I do not know how to provide the array TownList[100] to the sort function.
To pass an array of anything to a function, you can just pass a pointer to the first element plus an array length:
struct mystruct {
char* something;
/* ... */
}
struct mystruct myarray[100];
void do_something(struct mystruct* array, int length)
{
int i;
for (i=0; i<length; ++i)
{
array[i].something = ...
}
}
int main(void)
{
do_something(myarray, 100);
return 0;
}
Based on what you wrote, you would reference it in the caller and pass the result. For example:
void sort(char* str){
...
}
int main(){
for(int i=0; i<100; ++i){
sort(array[i].something);
}
}
Alternately you may want to pass the full array and handle it in another function, which would be like this:
void sort(struct mystruct * array){
...
}
int main(){
for(int i=0; i<100; ++i){
sort(array);
}
}
If, on the other hand, you meant that you want to sort the outer array of structures by the contents of something, then you would either have to pass the array as above and implement your own sort, or use a sorting function that takes a function pointer so you can write a comparator. The latter is available in the standard C library, and could be used something like this.
#include <stdlib.h>
int compare_mystruct_by_something(const void *a, const void *b){
return strcmp(((struct mystruct*)a)->something, ((struct mystruct *)b)->something);
}
int main(){
qsort(array, 100, sizeof(struct mystruct), compare_mystruct_by_something);
}