简体   繁体   English

二维指针数组C

[英]2D array of pointers C

This may be fairly easy to answer but i cant seem to figure out the syntax for it. 这可能很容易回答,但是我似乎无法弄清楚它的语法。 What i need is a 2D array the first element would be pointers to CSTRING's and the second element would be a counter. 我需要的是2D数组,第一个元素是CSTRING的指针,第二个元素是计数器。

The purpose of this is for threading and passing word lists to threads based on the thread number. 这样做的目的是根据线程号对单词列表进行线程化并将其传递给线程。 As in if it was defined something like char wordlists[100][10] (100 words and 10 lists 1 for each thread 10 total) i could initially fill all the lists up with words from a file and then pass a specific list to a thread to process and once a thread joins back to the main thread i would refill the word list related to the joining thread and relaunch the thread with new words. 就像定义了char wordlists [100] [10](每个线程10个单词,共10个列表,共10个单词)之类的东西一样,我最初可以用文件中的单词填充所有列表,然后将特定列表传递给线程进行处理,一旦线程重新加入主线程,我将重新填充与加入线程相关的单词列表,并使用新单词重新启动该线程。

Overall i dont get the syntax for making a 2D array of pointers (first element pointers second just a counter). 总的来说,我没有得到制作2D指针数组的语法(第一个元素指针仅次于一个计数器)。 Also is it possible to pass just the array address where the words for the particular thread starts at so i dont need to pass the entire 2D array of 1000 words? 也有可能只传递特定线程的单词开始处的数组地址,这样我就不需要传递整个1000个单词的2D数组了? (so that i can just pass the starting address where that threads 100 words are stored) (这样我就可以通过存储线程100个单词的起始地址)

thanks for the help! 谢谢您的帮助!

edit:: if you guys have any better suggestions for a similar way to process big word files via threads then do suggest them im open to more ideas. 编辑::如果你们对通过线程处理大单词文件的类似方法有更好的建议,那么建议他们对更多想法开放。

i dont get the syntax for making a 2D array of pointers 我没有获得制作2D指针数组的语法

* has a lower precedence than [] . *优先级比[]低。 Therefore wordlists is an array of array of pointer to char . 因此,单词表是一个指向char的指针数组的数组。

char *wordlists[100][10];

To declare a 2D array of pointers (to type T ): 声明一个二维指针数组(输入T ):

T *array[width][height];

Also is it possible to pass just the array address [...] so I don't need to pass the entire 2D array of 1000 words? 也可以只传递数组地址,所以我不需要传递整个1000个字的2D数组吗?

You couldn't even do anything else. 你什至无能为力。 In C, arrays are always passed by pointer, not by value. 在C语言中,数组始终通过指针而不是通过值传递。

You probably can benefit from reading the (relevant parts of, but i'd recommend the WHOLE of) the comp.lang.c FAQ . 阅读comp.lang.c FAQ (的相关部分,但我会推荐整个过程)可能会使您受益。

It is probably a mandatory read for anyone touching C (and C-like langages), and it is incredibly eye-opening (especially about undefined statements behavior, null pointers value and use, and, in your case, pointers and arrays of pointers). 对于任何接触C(和类似C的语言)的人来说,这可能都是强制性的阅读,而且令人难以置信(特别是关于未定义的语句行为,空指针的值和使用以及在您情况下的指针和指针数组) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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