im making a project and i was getting an error out of nowhere supposedly so i went into debug mode and couldn’t find the error but the error only occurred in a specific place so i copied the code and ran it and the error repeated. i couldn’t search as to why it was giving an error as far as i know this should work.
in the code below i tried with direct assigning and it worked but i want to know why the pointer dereference one didn’t work.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int assign(char**,char*);
int main(){
char **arr = calloc(9,sizeof(char*));
char *ele1 = "hello1";
assign(arr,ele1);
for(int i=0;i<2;i++){
printf("%sn",arr[i]);
}
}
int assign(char **arr,char *ele1){
for(int *i=0;*i<2;(*i)++){
arr[*i]=ele1;
}
// for(int i=0;i<2;i++){
// arr[i]=ele1;
// }
}
i commented the first 3 lines in assign function and uncommented the remaining and tried and it worked. here im only assigning 2 values but that shouldnt give any unknown behaviour
neo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.