[英]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[][]
我尝试将二维数组传递给函数string[][]
,然后在该函数中创建一个指向所传递数组的指针。我将比较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 我不想修改string[3][3]
,然后该函数将退出该函数而不返回任何内容
I have the idea, but I don't know how to declare pointer
, please help me. 我有这个主意,但是我不知道如何声明pointer
,请帮帮我。 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. 在C语言中,字符串表示为字符数组。 This is represented in two normal ways, char* identifier
and char identifier[]
. 这用两种常规方式表示, char* identifier
和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). 如评论中所述,请注意第二维(本例中为20
)。 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). C允许使用长度完全相同的字符串初始化char数组,在这种情况下,该数组最终将不包含字符串(即它将包含不以null终止的字符)。
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). 我不知道我是否理解正确,但是如果这样做,我将发布一个示例,您可以在其中传递“字符串”数组(在C中表示为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. 注意:*我将“ strcmp()== 1”更改为检查返回是否为正。 Checking for a specific value is unconventional (might not match your C implementation). 检查特定值是非常规的(可能与您的C实现不匹配)。 In the documentation, the only guarantee is that strcmp will return positive, negative or zero, but not a specific value. 在文档中,唯一的保证是strcmp将返回正数,负数或零,但不会返回特定值。 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. http://linux.die.net/man/3/strcmp *数组中必须至少有两个以null终止的字符串,my_function函数才能正常运行而不会出现段错误。
I hope this guides you in the right direction. 我希望这可以指导您正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.