0

I can not access the array from main in the function. How do i correct this ? Whenever I compile it say the argument 1 is of type **char and the argument 1 passed to fre in incompatible. Do i need to change any syntaxes ?

void fre(char *asd);

int main()

 {
   char *names[7]={"Jayan Tennakoon","John Long","Robert Lang"};

   int i;

   for(i=0;i<7;i++)
     {

        printf("%s\n",names[i]);

     }

     fre(names);

     return 0;

 }

 void fre(char *asd)
   {
     int i;

     for(i=0;i<7;i++)
       {

          printf("%s\n",asd[i]);

       }
 }
4
  • 1
    void fre(char *asd) --> void fre(char *asd[]) or void fre(char **asd) Commented May 4, 2016 at 17:00
  • 1
    ... and you could pass the number of names too, then fre will look rather like main(int argc, char *argv[]) Commented May 4, 2016 at 17:02
  • In main you have char *names[7], an array of pointers to char. You attempt to pass this array to function fre(). But fre is declared as accepting a single pointer to char. So it doesn't work. Commented May 4, 2016 at 17:05
  • Why do you have a blank line after every line ? Commented May 4, 2016 at 17:25

2 Answers 2

2

You've declared an array of 7 pointers to char. You initialized it with 3 of them. You need to pass an array of pointers, not a pointer to an array of chars. Based on the comments I'll add a few more ways to declare the arrays that might make it clear what the different ways actually mean.

#include <stdio.h>
#include <stdlib.h>

void fre(char *asd[]);

int main(void) {
  char * name  [ ] = {"Jayan Tennakoon","John Long","Robert Lang"};
  char * names [7] = {"Jayan Tennakoon","John Long","Robert Lang"};
  char   names2[7] = {'H', 'e', 'l','l','o','\0'};
  char **names3   = names;

  printf("names2 = %s\n",names2);
  printf("names3[0] = %s\n",names3[0]);
  int i;
  for(i = 0; i < 3; i++) {
    printf("%s\n",names[i]);
  }
  fre(names);
  return 0;

}

void fre(char *asd[]) {
  int i;
  for(i = 0; i < 3; i++) {
    printf("%s\n",asd[i]);
  }
}

I also had to reduce the loop from 7 to 3 or you would experience Undefined Behavior, if lucky this would be a segmentation fault, if unlucky it would likely have printed garbage but exited with a return statue of 0.

Sign up to request clarification or add additional context in comments.

15 Comments

Hi Harry. Thanks it worked perfectly. I have one question, why is it *char[] and not just char[] when i attempt to store the names ?
What do you mean store the names. Do you mean this char *names[7]={"Jayan Tennakoon","John Long","Robert Lang"};
Have a look at the names2 declaration and compare it with how names is declared etc.
Look at names2 that I added to the original answer. Note the difference between it and names. names is a pointer to a pointer, in this case we're allocating space for it by using [7].
why can't I just use char[]={"Arvind Asd"," John Lang", "Mike asdert"} ?
|
0

With

char *names[7]={"Jayan Tennakoon","John Long","Robert Lang"};

You have an array of pointers to char. So

void fre(char *asd)

should be

void fre( char *asd[], int n) 
/* it is good to pass the total(here 3) 
 * since you have initialized the first three elements
 */
{
.
.
for(i=0;i<n;i++)
.
.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.