Site Overlay

char * 实现的字符串列表(浪费内存型)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define kNumRow 20
#define kNumCol 255
#define kTotal kNumRow *kNumCol

char *getLinePtr(char *buffer, int lineNumber)
{
    return (buffer + (lineNumber - 1) * kNumCol);
}
int main()
{
    char *buffer = malloc(kTotal);
    memset(buffer, 0, kTotal);
    scanf("%s", getLinePtr(buffer, 1));
    scanf("%s", getLinePtr(buffer, 2));
    scanf("%s", getLinePtr(buffer, 3));
    printf("line: %s\n", getLinePtr(buffer, 3));
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注