简体   繁体   English

如何将二维数组的字符串拆分为一维integer

[英]How to split a string of a two dimensional array to one dimensional integer

I tried the code below, but the problem is that, I cannot get the last array in the end, any suggestion?我尝试了下面的代码,但问题是,我最终无法获得最后一个数组,有什么建议吗?

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

int main() {
    char *token;
    char str[20] = "{{0,1},{1,1},{45,56},{0,1}}";
    int* arr = malloc(sizeof(int) * 8);
    
    token = strtok(str, "},{");
    int count = 0;
    while(token != NULL){
        printf( " %s\n", token);
        arr[count] = atoi(token);
        token = strtok(NULL, "},{");
        count ++;
    }
    
    for (int i=0; i<8; i++){
        printf("%d\t",arr[i]);
    }
   
   return(0);
    
}

for example例如

int a[4][2];

sscanf("{{0,1},{1,1},{45,56},{0,1}}", "{{%d,%d},{%d,%d},{%d,%d},{%d,%d}}", 
        &a[0][0], &a[0][1], 
        &a[1][0], &a[1][1], 
        &a[2][0], &a[2][1], 
        &a[3][0], &a[3][1]);

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

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