在 C 中动态分配 const char 数组的正确方法
Correct way to dynamically allocate a const char array in C
我必须逐行读取文件并将这些行添加到 char 数组中。
我需要保留数组 const 并将其作为 const 指针传递。
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void takeAction(const char **charArray) {
for (int i = 0; i<2; i++) {
printf("%s\n", charArray[i]);
}
}
int main(int ac, char **av)
{
// Read patterns from file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
int counter = 0;
const char *charArray[1000];
fp = fopen(av[1], "r");
if (fp == NULL) {
fprintf(stderr, "%s File could not be opened \n", av[2]);
return 1;
}
while ((read = getline(&line, &len, fp)) != -1)
{
charArray[counter] = malloc(sizeof(char)*(read+1));
memcpy(charArray[counter], line, read);
counter++;
if (counter == 2) {
counter = 0;
takeAction(charArray);
}
}
fclose(fp);
return 0;
}
编译错误
temp.c: In function ‘main’: temp.c:42:16: warning: passing argument 1
of ‘memcpy’ discards ‘const’ qualifier from pointer target type
[-Wdiscarded-qualifiers]
memcpy(charArray[counter], line, read);
^~~~~~~~~ In file included from temp.c:3:0: /usr/include/string.h:42:14: note: expected ‘void * restrict’ but
argument is of type ‘const char *’ extern void *memcpy (void
*__restrict __dest, const void *__restrict __src,
执行
./temp MyFileWithMultipleLines
当我将 charArray
保持为 const
时,我在 memcpy
-> ‘memcpy’ discards ‘const’ qualifier
时收到警告
当我从 charArray
中删除 const
时,调用函数 takeAction
时出现错误。
您不能将 charArray
作为常量写入它。
尝试这样的事情:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void takeAction(const char **charArray) {
for (int i = 0; i<2; i++) {
printf("%s\n", charArray[i]);
}
}
int main(int ac, char **av)
{
// Read patterns from file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
int counter = 0;
char *charArray[1000];
fp = fopen(av[1], "r");
if (fp == NULL) {
fprintf(stderr, "%s File could not be opened \n", av[2]);
return 1;
}
while ((read = getline(&line, &len, fp)) != -1)
{
charArray[counter] = malloc(sizeof(char)*(read+1));
memcpy(charArray[counter], line, read);
counter++;
if (counter == 2) {
counter = 0;
takeAction((const char **)&charArray[0]);
}
}
fclose(fp);
return 0;
}
我必须逐行读取文件并将这些行添加到 char 数组中。 我需要保留数组 const 并将其作为 const 指针传递。
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void takeAction(const char **charArray) {
for (int i = 0; i<2; i++) {
printf("%s\n", charArray[i]);
}
}
int main(int ac, char **av)
{
// Read patterns from file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
int counter = 0;
const char *charArray[1000];
fp = fopen(av[1], "r");
if (fp == NULL) {
fprintf(stderr, "%s File could not be opened \n", av[2]);
return 1;
}
while ((read = getline(&line, &len, fp)) != -1)
{
charArray[counter] = malloc(sizeof(char)*(read+1));
memcpy(charArray[counter], line, read);
counter++;
if (counter == 2) {
counter = 0;
takeAction(charArray);
}
}
fclose(fp);
return 0;
}
编译错误
temp.c: In function ‘main’: temp.c:42:16: warning: passing argument 1
of ‘memcpy’ discards ‘const’ qualifier from pointer target type
[-Wdiscarded-qualifiers]
memcpy(charArray[counter], line, read);
^~~~~~~~~ In file included from temp.c:3:0: /usr/include/string.h:42:14: note: expected ‘void * restrict’ but
argument is of type ‘const char *’ extern void *memcpy (void
*__restrict __dest, const void *__restrict __src,
执行 ./temp MyFileWithMultipleLines
当我将 charArray
保持为 const
时,我在 memcpy
-> ‘memcpy’ discards ‘const’ qualifier
当我从 charArray
中删除 const
时,调用函数 takeAction
时出现错误。
您不能将 charArray
作为常量写入它。
尝试这样的事情:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void takeAction(const char **charArray) {
for (int i = 0; i<2; i++) {
printf("%s\n", charArray[i]);
}
}
int main(int ac, char **av)
{
// Read patterns from file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
int counter = 0;
char *charArray[1000];
fp = fopen(av[1], "r");
if (fp == NULL) {
fprintf(stderr, "%s File could not be opened \n", av[2]);
return 1;
}
while ((read = getline(&line, &len, fp)) != -1)
{
charArray[counter] = malloc(sizeof(char)*(read+1));
memcpy(charArray[counter], line, read);
counter++;
if (counter == 2) {
counter = 0;
takeAction((const char **)&charArray[0]);
}
}
fclose(fp);
return 0;
}