C 指针值为什么在变化?

C 指针值为什么在变化?,c,C,这是我的代码,我想读取frm txt文件并将字符串传递给print函数以处理它并打印它: #include<stdio.h> char *readfile(FILE *fp); void printstring(char *inputString); int main() { FILE *filePointer; filePointer=fopen("input.txt","r"); char *inp

这是我的代码,我想读取frm txt文件并将字符串传递给print函数以处理它并打印它:

 #include<stdio.h>
    char *readfile(FILE *fp);
    void printstring(char *inputString);
    int main()
    {
        FILE *filePointer;
        filePointer=fopen("input.txt","r");
        char *inputString=readfile(filePointer);
        printstring(inputString);

    }
    void printstring(char *inputString){
        int i=0;
        //char *ch[]=inputString;
        while((*(inputString+i))!='\0'){
            char c=(*(inputString+i));
            printf("%c",c);
            i++;
        }

    }


    char *readfile(FILE *fp){

        char c;
        int count;
        while((c=getc(fp))!=EOF){
        //printf("%c",c);
        count++;
        }
        char string[count+1];

        int i=0;
        rewind(fp);
        while((c=getc(fp))!=EOF){
        string[i]=c;
        i++;
        }
        string[i+1]='\0';
        char *chptr= &string[0];
        return chptr;
    }
输出:

nazi@nazi-laptop:~$ gcc -o rab RabinKrap.c -g
nazi@nazi-laptop:~$ ./rab
1�+nazi@nazi-laptop:~$
在while循环中赋值之后,如何将其重新初始化为其他内容

nazi@nazi-laptop:~$ gdb ./rab
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/nazi/rab...done.
(gdb) b printstring
Breakpoint 1 at 0x80484db: file RabinKrap.c, line 14.
(gdb) r
Starting program: /home/nazi/rab 

Breakpoint 1, printstring (inputString=0xbffff2e0 "12345\n\n1234567\004")
    at RabinKrap.c:14
14      int i=0;
(gdb) n
16      while((*(inputString+i))!='\0'){
(gdb) 
17          char c=(*(inputString+i));
(gdb) 
18          printf("%c",c);
(gdb) p inputString
$1 = 0xbffff2e0 "12345\n\n1234567\004"
(gdb) n
19          i++;
(gdb) 
16      while((*(inputString+i))!='\0'){
(gdb) p inputString
$2 = 0xbffff2e0 " \212-"
(gdb) 
nazi@nazi-笔记本电脑:~$gdb./rab
GNU gdb(Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1)7.4-2012.04
版权所有(C)2012免费软件基金会。
许可证GPLv3+:GNU GPL版本3或更高版本
这是自由软件:您可以自由更改和重新发布它。
在法律允许的范围内,不存在任何担保。键入“显示复制”
和“显示保修”了解详细信息。
此GDB配置为“i686 linux gnu”。
有关错误报告说明,请参阅:
...
阅读来自/家庭/纳粹/拉布的符号…完成。
(gdb)b打印字符串
断点1位于0x80484db:文件RabinKrap.c,第14行。
(gdb)r
启动程序:/home/NAZY/rab
断点1,printstring(inputString=0xbffff2e0“12345\n\n1234567\004”)
在拉宾克拉普。c:14
14 int i=0;
(gdb)n
16而((*(inputString+i))!='\0'){
(gdb)
17字符c=(*(inputString+i));
(gdb)
18 printf(“%c”,c);
(gdb)p输入字符串
$1=0xbffff2e0“12345\n\n1234567\004”
(gdb)n
19 i++;
(gdb)
16而((*(inputString+i))!='\0'){
(gdb)p输入字符串
$2=0xbffff2e0“\212——”
(gdb)

由于无法将poitner返回到本地数组,因此在函数返回后,该poitner将被解除分配,并且内容将丢失

试着这样,

char *
readfile(FILE *fp)
{
    char *content;
    char chr;
    size_t count;
    // Seek to the end of the file
    fseek(fp, 0L, SEEK_END);
    // Now, the current file position
    // is the size of the file
    count = ftell(fp);
    // Allocate space on the heap so that
    // it's still valid after returning from
    // this function
    content = malloc(count + 1);
    // Always check for errors, if this is
    // NULL there was no enough memory
    if (content == NULL)
        return NULL;
    // Reset the file pointer position
    rewind(fp);
    // Read `count' bytes into the buffer
    if (fread(content, 1, count, fp) != count) {
        // On failure, cleanup
        free(content);
        // Do not call `fclose()' because this
        // function did not call `fopen()'
        return NULL;
    }
    // '\0' Terminate the string (so it becomes A STRING)
    content[count] = '\0';
    // Finish, return it now
    return content;
}

由于无法将poitner返回到本地数组,因此在函数返回后,它将被解除分配,并且内容将丢失

试着这样,

char *
readfile(FILE *fp)
{
    char *content;
    char chr;
    size_t count;
    // Seek to the end of the file
    fseek(fp, 0L, SEEK_END);
    // Now, the current file position
    // is the size of the file
    count = ftell(fp);
    // Allocate space on the heap so that
    // it's still valid after returning from
    // this function
    content = malloc(count + 1);
    // Always check for errors, if this is
    // NULL there was no enough memory
    if (content == NULL)
        return NULL;
    // Reset the file pointer position
    rewind(fp);
    // Read `count' bytes into the buffer
    if (fread(content, 1, count, fp) != count) {
        // On failure, cleanup
        free(content);
        // Do not call `fclose()' because this
        // function did not call `fopen()'
        return NULL;
    }
    // '\0' Terminate the string (so it becomes A STRING)
    content[count] = '\0';
    // Finish, return it now
    return content;
}

您为什么不检查
fopen
的返回值?@EdHeal我没有收到您的问题
fopen
可以返回NULL。您需要对此进行测试。请参阅手册页。好的,这解释了我遇到的新错误。您为什么不检查
fopen
的返回值?@EdHeal我没有收到您的问题
fopen
可以返回NULL。您需要对此进行测试。请参阅手册页。好的,这解释了我遇到的新错误