pointer to an array in a function
Hi,
I wanted to find out how i would point to the array arr in function get(char *p) so that i ould print the contents of this array.
Your help will be appreciated.
Code:
int main(){
char * ptr = NULL:
get(ptr);
printf("contents: %s:, ptr);
}
void get(char *p){
char [5]mm = NULL;
int i;
for(ii = o;i <5;i++){
mm[i] = i;
}
p = mm; /*I'm not sure about this part */
}
Re: pointer to an array in a function
To correct your code the easy way, simply define ptr as an array, and fix the other syntactical errors:
Code:
int main(){
char ptr[100];
get(ptr);
printf("contents: %s:, ptr);
return 0;
}
void get(char *p){
// char [5]mm = NULL;
int i;
for(ii = o;i <5;i++){
ptr[ i ] = i;
}
// p = mm; /*I'm not sure about this part */
}
Also, if you are loading the array with integer values, why are you printing as a string?
It's not good form to post code you want help with that cannot possibly compile, as shown in red. You need to compile and run your code, then post what does not run properly.