Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Don';我不知道如何实现指向char的指针数组的指针_C_Arrays_Pointers - Fatal编程技术网

Don';我不知道如何实现指向char的指针数组的指针

Don';我不知道如何实现指向char的指针数组的指针,c,arrays,pointers,C,Arrays,Pointers,我对指针不在行。我知道的足够多,可以让指向char的指针数组工作,如下面的第一个示例所示。但我不想传递整个指针数组,因为它占用了堆栈上太多的空间。我想做的是将一个指针传递给为指针数组分配的内存。我不知道怎么做 该程序的工作原理是: #include "pch.h" #include "$StdHdr.h" #include "TmpTstPtr1.h" #define SRC_LIN_SIZ 150 int main(int ArgCnt, char * ArgVal[]) { ch

我对指针不在行。我知道的足够多,可以让指向char的指针数组工作,如下面的第一个示例所示。但我不想传递整个指针数组,因为它占用了堆栈上太多的空间。我想做的是将一个指针传递给为指针数组分配的内存。我不知道怎么做

该程序的工作原理是:

#include "pch.h"
#include "$StdHdr.h"
#include "TmpTstPtr1.h"

#define SRC_LIN_SIZ 150

int main(int ArgCnt, char * ArgVal[])
{
    char        InpFilPth[MAX_PATH + 1];
    FILE        * InpFilPtr;
    char        ** SrcArr;
    unsigned    Sub1;
    unsigned    SrcArrCnt = 0;

    strncpy_s(InpFilPth, "TmpTstPtr1.cpp", strlen("TmpTstPtr1.cpp"));
    fopen_s(&InpFilPtr, InpFilPth, "r");
    SrcArr = (char **)malloc(999999 * sizeof(char *));
    LodSrcArr(InpFilPtr, SrcArr, &SrcArrCnt);
    for (Sub1 = 0; Sub1 < SrcArrCnt; Sub1++) {
        printf("SrcArr[%d] = %s\n", Sub1, SrcArr[Sub1]);
    }
    fclose(InpFilPtr);

    return 0;
}

void LodSrcArr(FILE * InpFilPtr, char ** SrcArr, unsigned * SrcArrCnt)
{
    char        SrcLin[SRC_LIN_SIZ + 1];
    char        * GetStrPtr;


    GetStrPtr = GetStr(SrcLin, SRC_LIN_SIZ, InpFilPtr);
    while (GetStrPtr != NULL) {
        SrcArr[*SrcArrCnt] = (char *)malloc(SRC_LIN_SIZ + 1);
        //      CpySiz(SrcArr[*SrcArrCnt], strlen(SrcLin) + 1, SrcLin);
        errno = strncpy_s(SrcArr[*SrcArrCnt], SRC_LIN_SIZ + 1, SrcLin, strlen(SrcLin));
        (*SrcArrCnt)++;
        GetStrPtr = GetStr(SrcLin, SRC_LIN_SIZ, InpFilPtr);
    }
}


char * GetStr(char * Str, const int MaxChr, FILE * InpFilPtr)
{
    char        * InpRtnVal = NULL;
    unsigned    Sub1;

    //  Get string from input file.  Find the end of the string if something entered.
    InpRtnVal = fgets(Str, MaxChr + 1, InpFilPtr);
    if (InpRtnVal != NULL) {
        Sub1 = 0;
        while (Str[Sub1] != '\n' && Str[Sub1] != '\0') {
            Sub1++;
        }
        //  Replace newline with null.
        if (Str[Sub1] == '\n') {
            Str[Sub1] = '\0';
        }
    }
    return InpRtnVal;
#包括“pch.h”
#包括“$StdHdr.h”
#包括“TmpTstPtr1.h”
#定义SRC_LIN_SIZ 150
int main(int ArgCnt,char*ArgVal[]
{
char InpFilPth[MAX_PATH+1];
文件*InpFilPtr;
字符**SrcArr;
无符号Sub1;
无符号SrcArrCnt=0;
strncpy_s(InpFilPth,“TmpTstPtr1.cpp”,strlen(“TmpTstPtr1.cpp”);
fopen_s(&InpFilPtr,InpFilPth,“r”);
SrcArr=(char**)malloc(999999*sizeof(char*);
lodscarr(InpFilPtr、SrcArr和SrcArrCnt);
对于(Sub1=0;Sub1
以下程序甚至无法实现:

#include "pch.h"
#include "$StdHdr.h"
#include "TmpTstPtr2.h"

#define SRC_LIN_SIZ 150

int main(int ArgCnt, char * ArgVal[])
{
    char        InpFilPth[MAX_PATH + 1];
    FILE        * InpFilPtr;
    char        ** SrcArr;
    unsigned    Sub1;
    unsigned    SrcArrCnt = 0;
    char        *** SrcArrPtr = NULL;

    strncpy_s(InpFilPth, "TmpTstPtr2.cpp", strlen("TmpTstPtr2.cpp"));
    fopen_s(&InpFilPtr, InpFilPth, "r");
    SrcArr = (char **)malloc(999999 * sizeof(char *));
    SrcArrPtr = &SrcArr;
    LodSrcArr(InpFilPtr, SrcArrPtr, &SrcArrCnt);
    SrcArrPtr = &SrcArr;
    for (Sub1 = 0; Sub1 < SrcArrCnt; Sub1++) {
//      printf("SrcArr[%d] = %s\n", Sub1, SrcArr[Sub1]);    // got "Exception thrown: read access violation. it was 0xCDCDCDCD."
        printf("SrcArr[%d] = %s\n", Sub1, **SrcArrPtr);     // get 75 lines of garbage
        (**SrcArrPtr) += sizeof(char *);
    }
    fclose(InpFilPtr);

    return 0;
}


void LodSrcArr(FILE * InpFilPtr, char *** SrcArrPtr, unsigned * SrcArrCnt)
{
    char        SrcLin[SRC_LIN_SIZ + 1];
    char        * GetStrPtr;


    GetStrPtr = GetStr(SrcLin, SRC_LIN_SIZ, InpFilPtr);
    //  while (GetStrPtr != NULL and *SrcArrCnt == 0) {
    while (GetStrPtr != NULL) {
        **SrcArrPtr = (char *)malloc(SRC_LIN_SIZ + 1);
        //      CpySiz(SrcArr[*SrcArrCnt], strlen(SrcLin) + 1, SrcLin);
        errno = strncpy_s(**SrcArrPtr, SRC_LIN_SIZ + 1, SrcLin, strlen(SrcLin));
        (**SrcArrPtr) += sizeof(char *);
        (*SrcArrCnt)++;
        GetStrPtr = GetStr(SrcLin, SRC_LIN_SIZ, InpFilPtr);
    }
}


char * GetStr(char * Str, const int MaxChr, FILE * InpFilPtr)
{
    char        * InpRtnVal = NULL;
    unsigned    Sub1;

    //  Get string from input file.  Find the end of the string if something entered.
    InpRtnVal = fgets(Str, MaxChr + 1, InpFilPtr);
    if (InpRtnVal != NULL) {
        Sub1 = 0;
        while (Str[Sub1] != '\n' && Str[Sub1] != '\0') {
            Sub1++;
        }
        //  Replace newline with null.
        if (Str[Sub1] == '\n') {
            Str[Sub1] = '\0';
        }
    }
    return InpRtnVal;
}
#包括“pch.h”
#包括“$StdHdr.h”
#包括“TmpTstPtr2.h”
#定义SRC_LIN_SIZ 150
int main(int ArgCnt,char*ArgVal[]
{
char InpFilPth[MAX_PATH+1];
文件*InpFilPtr;
字符**SrcArr;
无符号Sub1;
无符号SrcArrCnt=0;
char***SrcArrPtr=NULL;
strncpy_s(InpFilPth,“TmpTstPtr2.cpp”,strlen(“TmpTstPtr2.cpp”);
fopen_s(&InpFilPtr,InpFilPth,“r”);
SrcArr=(char**)malloc(999999*sizeof(char*);
SrcArrPtr=&SrcArr;
lodscarr(InpFilPtr、SrcArrPtr和SrcArrCnt);
SrcArrPtr=&SrcArr;
对于(Sub1=0;Sub1
正如评论所说,当我试图通过下标访问
SrcArr
时,我会得到一个运行时错误。当我试图通过指针访问时,我会得到垃圾。问题可能是我说
SrcArrPtr=&SrcArr;
。我不知道它是否重要,但后面每行打印的垃圾短4个字符。就好像它是实际上是打印指针数组本身,而不是它们指向的字符串。我不知道

我将其编码为上述代码的原因是为了让程序能够编译。我以前从未尝试过使用3个指针。我尝试做的事情可能吗?如果可能,有人能告诉我如何做吗?解释一下它的工作原理会很好,但不是必需的。(我使用的是Visual Studio 2017,但我认为这并不重要。)

TIA.

#包括
#包括
#包括
void foo(char*bar[10]){//一个实数组
对于(int i=0;i<10;++i){
bar[i]=calloc(2,1);
条形图[i][0]=“0”+i;
}
}
void xox(char**qux){//指向堆上某些char指针的指针
对于(int i=0;i<10;++i){
qux[i]=calloc(2,1);
qux[i][0]=“0”+i;
}
}
内部主(空)
{
char*bar[10];//一个“实”数组
富(巴);;
对于(尺寸i=0;i<10;++i)
看跌期权(bar[i]);
putchar('\n');
//清理:
对于(尺寸i=0;i<10;++i)
自由(bar[i]);
//方案b:
char**qux=calloc(10,sizeof(*qux));
xox(qux);
对于(尺寸i=0;i<10;++i)
看跌期权(qux[i]);
putchar('\n');
//清理:
对于(尺寸i=0;i<10;++i)
自由(qux[i]);
免费(qux);
}
我想做的是通过考试
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

void foo(char* bar[10]) { // a real array
    for (int i = 0; i < 10; ++i) {
        bar[i] = calloc(2, 1);
        bar[i][0] = '0' + i;
    }
}

void xox(char **qux) { // pointer to some char-pointers on the heap
    for (int i = 0; i < 10; ++i) {
        qux[i] = calloc(2, 1);
        qux[i][0] = '0' + i;
    }
}

int main(void)
{
    char* bar[10]; // a "real" array
    foo(bar);

    for (size_t i = 0; i < 10; ++i)
        puts(bar[i]);
    putchar('\n');

    // cleanup:
    for (size_t i = 0; i < 10; ++i)
        free(bar[i]);

    // plan b:
    char **qux = calloc(10, sizeof(*qux));
    xox(qux);

    for (size_t i = 0; i < 10; ++i)
        puts(qux[i]);
    putchar('\n');

    // cleanup:
    for (size_t i = 0; i < 10; ++i)
        free(qux[i]);
    free(qux);
}
int *integers = (int*)malloc(4 * sizeof(int));
int **pointers = (int**)malloc(4*sizeof(int*));
pointers[0] = &integers[0];
pointers[1] = &integers[1];
pointers[2] = &integers[2];
pointers[3] = &integers[3];
int* pointers[4];
pointers[0] = &integers[0];
pointers[1] = &integers[1];
pointers[2] = &integers[2];
pointers[3] = &integers[3];
int **ppointer = pointers;
char *ptrs[N];
foo( ptrs );
foo( &ptrs[0] );
void foo( char **ptrs )
void foo( char *ptrs[] )
void foo( char *ptrs[N] )
SrcArr = (char **)malloc(999999 * sizeof(char *));
SrcArr = malloc( 999999 * sizeof *SrcArr ); // you sure you need that many elements??!
T *p = malloc( N * sizeof *p );
T *p;
...
p = malloc( N * sizeof *p );