标题:请问我编程的这个可以更加简单化吗?
只看楼主
khndhkx123
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2016-3-7
结帖率:0
 问题点数:0 回复次数:2 
请问我编程的这个可以更加简单化吗?
小弟练习时编辑了一段,请问可以更加省略或者简单化吗?


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

int main(int argc, char *argv[]){

        int n;
        int i;
        char **name;

        n=atoi(argv[1]);

        printf("enter %d names :\n",n);

        name=(char**)malloc(sizeof(char*)*n);

        for(i=0;i<n;i++){
                name[i]=(char*)malloc(sizeof(char)*30);
        }

        for(i=0;i<n;i++){
                scanf("%s",name[i]);
        }
      printf("the names you entered :\n");

        for(i=0;i<n;i++){
                printf("%s\n",name[i]);
        }

        return 0;

}
搜索更多相关主题的帖子: include names 
2016-03-08 10:02
alice_usnet
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:370
专家分:2020
注 册:2016-3-7
得分:0 
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int n,i;
    char **name;
    n=atoi(argv[1]);
    printf("enter %d names :\n",n);
    name=(char**)malloc(sizeof(char*)*n);
    for(i=0;i<n;i++){
        name[i]=(char*)malloc(sizeof(char)*30);
        scanf("%s",name[i]);
    }
    printf("the names you entered :\n");
    for(i=0;i<n;i++){
        printf("%s\n",name[i]);
    }
    free(name);
    return 0;
}

未佩好剑,转身便已是江湖
2016-03-08 22:16
china圻炻海
Rank: 2
等 级:论坛游民
帖 子:4
专家分:30
注 册:2016-3-8
得分:0 
回复 楼主 khndhkx123
就功能来说:
#include <stdio.h>
#include <stdlib.h>
#define NULL (ushort)0
int main(int argc, char *argv[]){

        int n = 0;
        int i = 0;
        char *name = NULL;
         char *pc = NULL;

        n = atoi(argv[1]);

        printf("enter %d names :\n", n);

        name=(char*)malloc(sizeof(char) * n * (30 + 1));

        for(name; name < (pc + (sizeof(char) * n * (30 + 1))); name += (30 + 1))
         {
             if (NULL == pc)
             {
                printf("内存不足!\n");
                n = 0;
                break;
             }
            scanf("%s", pc[i++]);
        }

         printf("the names you entered :\n");
            
        for(i=0; i<n; i++)
         {
            printf("%s\n", pc[i]);
        }

         free (pc);

        return 0;
}
2016-03-08 23:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-462249-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.408548 second(s), 9 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved