AVIK DUTTA
AVIK DUTTA

Reputation: 746

why is the pointer value changing?

here is my code , i want to read frm txt file and pass the string to print function to process it an print it:

 #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;
    }

input file contents :

12345

1234567

output:

nazi@nazi-laptop:~$ gcc -o rab RabinKrap.c -g
nazi@nazi-laptop:~$ ./rab
1�+nazi@nazi-laptop:~$

some how after assignment in the while loop its reinitialized to something else.

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) 

Upvotes: 0

Views: 65

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53016

Because you can't return a poitner to a local array, it gets deallocated and the contents are lost after the function returns

Try like this,

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;
}

Upvotes: 2

Related Questions