简体   繁体   中英

How can I create an array of pointers point to a 2 dimensional array

I really don't know how to explain it. Please bear with me.
I try to pass the 2 dimensional array into a function string[][] , and in that function I going to create a pointer which point to the array I passed in. I will do compare of element in string[][]

//From main
char string[3][3] = {"cat","dog","bat"};
//From function
declare a pointer which points to string;
if (strcpm(string[1],string[2]) == 1)
    {
        pointer[1] value set to address of string[2];
        pointer[2] value set to address of string[1]
    }
printf("%s", *(pointer));

I don't want to modify string[3][3] , then the function would quit the function without return anything
I have the idea, but I don't know how to declare pointer , please help me. Thanks! Visualize

In main                     In function
string                      ptr
cat                         1000
dog                         1008
bat                         1012
Do compare
dog > bat
ptr[3] = address of dog;
ptr[2] = addess of bat;
printf("%s", *(ptr+1);
printf("%s", *(ptr+2);
quit function, return nothing

in C, strings are represented as an array of characters. This is represented in two normal ways, char* identifier and char identifier[] . A 2 dimensional Array is declared as a pointer to the string. So, it can be written as

char **strArray1
char  *strArray2[]
char   strArray3[][]

Each has different implications, that I won't get into here as it's pedantic.

To answer your question as literally as I can, you can write the code in your post as

int main (void) {
    char * strArray[2] = { "This is a String", "This is a String too" };
    if (strcmp(strArray[0], strArray[1]) == 0) {
       ...
    }
    return 0;
}

For a function to accept an array of strings (not an array of pointers to strings, which is different):

void func(size_t rows, size_t cols, char strings[rows][cols])
{
    char const *pointers[rows];

    // Initialize each pointer to point to each row
    for (size_t i = 0; i < rows; ++i)
         pointers[i] = &strings[i][0];

    // here you can manipulate the pointers, e.g. bubble sort them

    printf("%s\n", pointers[0]);
}

Calling this function:

int main()
{
    char stuff[4][20] = { "cat", "dog", "snuffleupagus", "horse" };

    func(4, 20, stuff);
}

Pro tip: you can compute array dimensions instead of using magic numbers:

#define LENGTH_OF(array) ( sizeof(array) / sizeof((array)[0]) )

char stuff[][20] = { "cat", "dog", "snuffleupagus", "horse" };
func( LENGTH_OF(stuff), LENGTH_OF(stuff[0]), stuff );

As noted in comments, be careful about the second dimension ( 20 in my case). C permits an array of char to be initialized with a string of exactly that length, in which case the array will end up not containing a string (ie it will contain characters that are not null-terminated).

I don't know if I understand correctly, but if I do, I am posting an example where you can pass an array of "strings" (which in C is represented as an array of arrays of char). Then you can compare the two first strings in your array

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

char* my_function(char *strings[]) {
    char *ptrToString = NULL;

    if (strcmp(strings[0],strings[1]) > 0)   // Do you really mean '==1' here?
    {
        ptrToString = strings[1];
    } else {
        ptrToString = strings[0];
    }

    printf ("%s\n", ptrToString);   // No need for '*', %s takes a pointer to char as argument

    return ptrToString;
}

void main (void) {
   char *a[] = {"dog", "cat"};
   my_function(a);
}

Notes: * I changed "strcmp() == 1" to checking if the return is possitive or not. Checking for a specific value is unconventional (might not match your C implementation). In the documentation, the only guarantee is that strcmp will return positive, negative or zero, but not a specific value. http://linux.die.net/man/3/strcmp * There must be at least two null-treminated strings in the array for my_function function to work without segfaulting.

I hope this guides you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM