Below I have two code samples. In the first I can only work with the dot . operator and in the second I can only work with the arrow -> operator. But why? Do I not have in both cases pointers?
The code for arrow -> operators:
#include <stdlib.h>
#include <stdio.h>
typedef struct{
double **plist;
} ParticleList;
void sendPar(ParticleList *pl, int *n, int np){
pl -> plist = malloc(sizeof(double*) * np);
for(int p=0; p<np; p++){
pl->plist[p] = malloc(sizeof(double) * n[p]);
}
for(int p=0; p<np; p++){
for(int k=0; k<n[p]; k++){
pl->plist[p][k] = k+1;
}
}
}
int main(){
ParticleList pl;
int np = 3;
int n[np];
n[0] = 2;
n[1] = 4;
n[2] = 7;
sendPar(&pl, n, np);
}
The code for dot . operators:
#include <stdlib.h>
#include <stdio.h>
typedef struct{
double **plist;
} ParticleList;
void sendPar(int *n, int np){
ParticleList pl;
pl.plist = malloc(sizeof(double*) * np);
for(int p=0; p<np; p++){
pl.plist[p] = malloc(sizeof(double) * n[p]);
}
for(int p=0; p<np; p++){
for(int k=0; k<n[p]; k++){
pl.plist[p][k] = k+1;
}
}
free(pl.plist);
}
int main(){
int np = 3;
int n[np];
n[0] = 2;
n[1] = 4;
n[2] = 7;
sendPar(n, np);
}
Are both versions valid or is there any difference?