首先,我还是C语言的新手,并且对编程没有经验。对于您的解决方案,我将不胜感激。这是我上一个问题的一个后续问题。
我想将节点的neighborAddress及其权重存储在位于我的struct节点内部的动态数组中:
typedef struct node{
int port;
int ownAddress;
unsigned short *neighAddr;
unsigned int *neighWeight;
unsigned int total_neighs;
struct node *neighbors_array; //unsure about type
} node;
命令行参数如下:
./node 8888 1 23:2 46:1 688:92 12:4
以':'分隔的参数可以无限期地重复,并且是一对作为neighbourAddress:weight给出的对。我需要将其存储在数组中。 这是我所拥有的:
[...]
/* to read neighbourAddress and weight separated by ':' */
int i, neighbourAddress, weight;
for(i = 3; i < argc; ++i){
if(sscanf(argv[i], "%d:%d", &neighbourAddress, &weight) != 2){
fprintf(stderr, "Wrong format. Format must be as follows: int:int.\n");
exit(EXIT_FAILURE);
}
/* store neighbour data in an array */
如您所见,我已经检索到了neighbourAddress和weight的值,但只需要将其存储到数组中即可。