Thread: char pointer

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    char pointer

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int getsize(void){
    int counter=0;
    FILE *testa;
    testa=fopen("error.txt","r");
    char *c;
       while(fscanf(testa,"%s",c)!=EOF) counter++;
    	printf("%s",c);
      return counter;
    }
    int main(){
    int size;
    size=getsize();
    return 0;
    }
    it compiles but it tells me segmantation fault any help?
    the txt file contains numbers with a space of each one

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, indent your code. Next, you should check if fopen succeeded, and eventually fclose if it did. Oh, and if you want to read a string, you need to allocate space for it first. (Plus naming a string c is quite misleading: I think to think it is a character rather than a string.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    fopen is succeed even if i put fclose still the same problem
    also c is a pointer of string so like i know it can still point to string even if i do not malloc

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cable
    also c is a pointer of string so like i know it can still point to string even if i do not malloc
    At the moment there is simply no space allocated, with or without malloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    so if i still want to keep the pointer of string how can i solve the problem
    i mean with minimum changes

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    What? Without malloc? Do you understand how pointers work? If it's used uninitialized, it most likely points to zero, or a garbage value left in the stack from previous use. Trying to shove data into these segments will fail unless you get lucky enough to have it pointing to an unused segment.

    Either you have to use a stack array:

    Code:
    char c[BUFSIZ];
    or suck it up and use malloc:

    Code:
    char *c = malloc(BUFSIZ);
    replacing BUFSIZ with however much you need. Also, if you're trying to read numbers, why are you fscanf()'ing for space-delimited words?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting a pointer to char to a pointer to int
    By kkk in forum C Programming
    Replies: 5
    Last Post: 05-18-2011, 11:54 AM
  2. Copy char array to char pointer
    By Suseela in forum C Programming
    Replies: 9
    Last Post: 08-06-2009, 12:49 PM
  3. pointer to pointer that points to a char array
    By steve1_rm in forum C Programming
    Replies: 2
    Last Post: 01-14-2009, 12:03 AM
  4. Pointer to String and Pointer to Char
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 03:03 PM
  5. Char-array vs Char-pointer
    By ripper079 in forum C++ Programming
    Replies: 12
    Last Post: 09-09-2002, 01:16 AM