3

This code take 5 strings and sort them in ascending way.

 void swap (char data[5][255], int i, int j) {
     char temp[255];
     strcpy(temp,data[i]);
     strcpy(data[i],data[j]);
     strcpy(data[j],temp);
 }

 void sort (char data[5][255], int n) {
     // * : first address contact
     int i, j;
     for(i = 0; i < n-1; i++)
         for( j = i+1; j > 0; j--)
             if(strcmp(data[j-1],data[j])>0)
             {
                 printf("%s",data[j-1]);
                 swap(data, j-1, j);
             }
 }

 int main() {
     char strings[5][255];
     char comp[255];
     int i, n;
     n = sizeof(strings)/sizeof(comp);
     printf("Enter 5 strings, max 255 chars each:\n");
     for(i=0; i < n; i++)
         scanf("%s",strings[i]);
     sort(strings, n);
     printf("Sorted data:\n");
     for(i=0; i < n-1; i++)
         printf("%s, ",strings[i]);
     printf("%s.\n",strings[i]);
     return 0;
 }

In addition of that, how can I possibly parse my static array string[5][255] to function by using pointer? I tried that, for example,

void sort ( char **data, int i ) { ... }

but it throws out error like this.

incompatible pointer types passing 'char [5][255]' to parameter of type 'char **'

Is there anything I can parse my array using pointer?

Since array parsed to function its first address(pointer), I thought function will accept those expression. Please give me some advice to understand.

Kariamoss
  • 542
  • 1
  • 9
  • 29
sogo
  • 351
  • 5
  • 20
  • why **data wouldn't work? unlike char *data[255], Jekyll? – sogo Dec 17 '13 at 23:34
  • 1
    http://stackoverflow.com/questions/12674094/array-to-pointer-decay-and-passing-multidimensional-arrays-to-functions << see here – Jekyll Dec 17 '13 at 23:38

4 Answers4

4

The parameter you must pass is not a pointer to a pointer char**, but a pointer to a char array char(*)[255]

void sort (char (*data)[255], int n)

When you pass an array, you can omit the size of the first dimension

void sort (char data[][255], int n)

which is equivalent to char(*)[255].

char** is a pointer, which points to another pointer. Whereas char(*)[255] is a pointer, which points to an array.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Thanks Mr. Olaf, but I can not understand why it is impossible. Is that because of array saving priority to do not allocate parsing parameters as an infinite way? – sogo Dec 17 '13 at 23:38
  • It is not possible, because these are different types. Please see the updated answer. – Olaf Dietsche Dec 17 '13 at 23:41
  • Thanks a lot Mr. Olaf, to sum up, function can not be point a pointer, but can point any other type (like array), right? – sogo Dec 17 '13 at 23:46
  • 1
    You can use pointer to a pointer as @Jekyll shows. In your case however, you just don't have one, but a pointer to array. – Olaf Dietsche Dec 17 '13 at 23:49
  • it's not `char *[255]`, but `char (*)[255]`. They are very different. – newacct Dec 18 '13 at 22:54
1

As I wrote you in the comment char[][] doesn't decay to char** but it decays to char(*)[] ( char (*data)[255] in your case) as the first element decay in a pointer which is not a "pointer to a pointer" but a pointer to an array of 255 characters. It is possible to use a char** pointer if you do something like this (c++):

char **array = new char *[N];

for(int i = 0; i<N; i++)
    array[i] = new char[N];

or using the malloc (c).

As @newact suggests it is important to distinguish between

char *data[255] -> char *[255]data = => "data is an array of 255 pointers to char" And

char (*data)[255] -> char [255](*data) => "data is a pointer to an array of 255 chars"

Jekyll
  • 1,434
  • 11
  • 13
1

You have to understand the difference between: char **data vs char (*data)[255]

Those are two different types because the allocation of memory may be different:

When char **data is used pointer arithmetic may not work properly, meaning data could be scattered all over the memory

When char (*data)[255] is used pointer arithmetic works perfectly because all elements of array are adjacent to each other

Darkhan
  • 1,258
  • 2
  • 13
  • 21
  • Thanks Mr. Daveel. Then if I assign array by using memory allocation like 'malloc', then is it possible to parse to double pointing function like, 'sort(char **data, int n)'? Because I remember I have tried parse dynamic array to pointer. – sogo Dec 17 '13 at 23:51
  • 1
    @Sogo Yes, if you will allocate your strings on the heap, then your sort(char **data, int n) will work – Darkhan Dec 18 '13 at 00:03
  • `char *data[255]` is not `char (*data)[255]` – newacct Dec 18 '13 at 22:55
-1

Try to make the following changes:

void swap ( char *data, int i, int j)

*(data + index1*255 + index2)
Rubens
  • 14,478
  • 11
  • 63
  • 92
SliceSort
  • 357
  • 3
  • 5