迭代-嵌套for循环
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//双层for循环
char *nestedForLoop(int n){
//nested --嵌套
int size = n * n * 26 + 1;
char * res = malloc(size * sizeof(char));
for(int i = 1; i <= n; i++){
for(int j =1; j<= n; j++){
char tmp[26];
//snprintf()函数是为了防止缓冲区溢出,并把printf的输出内容格式化!
snprintf(tmp, sizeof(tmp), "(%d,%d),", i, j);
//往res后面添加(连接)字符串tmp
strncat(res, tmp, size-strlen(res)-1);
}
}
return res;
}
int main(){
int n = 5;
char * res = nestedForLoop(n);
printf("%s", res);
return 0;
}