Thread: simple pointer

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Post simple pointer

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main(void)
    {
    
    char sentence[256];
    printf("\nEnter the sentence : ");
    fgets(sentence,255,stdin);
    fputs(sentence,stdout);
    }
    this code works fine...

    but when i tried with pointers,say
    Code:
     
    char *sentence;
    printf("\nEnter the sentence : ");
    fgets(sentence,255,stdin);
    fputs(sentence,stdout);
    it isnt working...don think my question as silly...i am new to c and pointers...help me plz..

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by cheeta View Post
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main(void)
    {
    
    char sentence[256];
    printf("\nEnter the sentence : ");
    fgets(sentence,255,stdin);
    fputs(sentence,stdout);
    }
    this code works fine...

    but when i tried with pointers,say
    Code:
     
    char *sentence;
    printf("\nEnter the sentence : ");
    fgets(sentence,255,stdin);
    fputs(sentence,stdout);
    it isnt working...don think my question as silly...i am new to c and pointers...help me plz..
    A pointer is an address. If you don't have the pointer pointing to a valid address you are going to get a segfault. You either make the pointer point to a statically allocated space or to a dynamically allocated space.

    Like so:

    Code:
    /*find a chunk of free memory on the heap and make this pointer point to it*/
    char *ptr = malloc(sizeof(char) * 101); 
    
    fgets(ptr,100,stdin);
    Last edited by claudiu; 04-28-2010 at 06:17 AM.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Just for reference, you do not need to subtract one from your array size when you pass it to fgets(). That function understands the need for null termination, so you can just tell it the whole size of your array.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Cool, thanks for clarifying that cas, I didn't remember if that was how fgets worked or not. Hopefully, that will clear the problem for the OP.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Simple pointer problem...
    By DavidDobson in forum C Programming
    Replies: 7
    Last Post: 08-19-2008, 02:33 AM
  3. Simple pointer question
    By sweets in forum C++ Programming
    Replies: 9
    Last Post: 11-16-2004, 08:48 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Simple structure + pointer question
    By gogo in forum C Programming
    Replies: 4
    Last Post: 10-25-2001, 07:05 AM