0

I'm making a simple program about sorting five teams from 1st to 5th.. but i wanted to make it by using Array of pointers & passing structures methods. here is what i've done..

#include <stdio.h>
#include <string.h>


struct league 
{
 char team1[20]; 
 char team2[20]; 
 char team3[20];
 char team4[20];
 char team5[20];
};


char *Arrange(struct league *table)

{
 struct league *Ptable[5] = {0};
  int i;

  Ptable[0]-> team5;
  Ptable[1]-> team2;
  Ptable[2]-> team3;
  Ptable[3]-> team1;
  Ptable[4]-> team4;

 for (i = 0; i < 5; i++)

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

return Ptable[i];
}


int main()

{
struct league table;

 strcpy(table.team1,"Arsenal");
 strcpy(table.team2,"Man City");
 strcpy(table.team3,"LiverPool");
 strcpy(table.team4,"Totenham");
 strcpy(table.team5,"Chelsea");

printf("League table:\n");

 Arrange(&table);


return 0;
}  

when i compile it i get this error:

warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘struct league **’ [-Wformat=]
   printf("%s\n", &Ptable[i]);

what is the right way to code this without making too many changes on my code? because i want to be using array of pointers with structures in a code like this.

2
  • This code doesn't do what you think it does. Ptable[i] is a pointer to a struct league. Each league struct has 5 members each of which are 20 character arrays. The five Ptable[i}->teami statements don't do anything. Commented Mar 9, 2017 at 1:48
  • Note that it is Tottenham Hotspurs (Spurs) — two t's in the middle. And Liverpool; no capital in the middle. Commented Mar 9, 2017 at 4:28

1 Answer 1

1

Each team in the league is a string, not a structure, so you don't need an array of structure pointers, just an array of pointers to the strings.

char *Arrange(struct league *table) {
    char *Ptable = malloc(5 * sizeof(char *));
    Ptable[0] = table->team5;
    Ptable[1] = table->team2;
    Ptable[2] = table->team3;
    Ptable[3] = table->team1;
    Ptable[4] = table->team4;
    for (i = 0; i < 5; i++) {
        printf("%s\n", &Ptable[i]);
    }
    return Ptable;
}

To return an array, you need to allocate it dynamically with malloc(), then return that pointer.

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

4 Comments

thank you.. it worked, i have try it before with just an array of pointers but i wrote it like this Ptable[0] = team5; etc.. i didn't put table->.. i didn't understand your last sentence about returning Ptable, but i returned Ptable[i] and it worked too.
@whiteknights After the loop, i = 5. There is no Ptable[i], the valid elements are Ptable[0] through Ptable[4].
thank you.. isn't Ptable[0] just one element? and i want to return all of the elements.
You can't return a local array, you need to allocate it dynamically with malloc(). I've updated the answer to show this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.