Help with pointers

This is a discussion on Help with pointers within the C Programming forums, part of the General Programming Boards category; Can someone tell me why my code isn't printing "it worked" when I input 'In' into the console? Code: int ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    20

    Help with pointers

    Can someone tell me why my code isn't printing "it worked" when I input 'In' into the console?
    Code:
    int main (int argc, char *argv[]) {
    	int i;
    	char* input="";
        struct node2d *p2d=NULL;
        for (i=argc-1; i>0; i--) {
    	p2d = make2dnode(argv[i], p2d);
        };
        print2d(p2d);
    	printf("Enter command: ");
    	input = getchar();
    	if(input=='I' && (input+1)=='n')
    		printf("it worked");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    Why are you using getchar to take the input command? I'd rather store it into a char array[] and then check if array[0] == 'I' && array[1] == 'n' && array[2] == '\0';

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    Well suppose I don't know how long the input is going to be. I can't specify a specific array size in that case, can I?

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    char* input="";

    ...

    input = getchar();
    if(input=='I' && (input+1)=='n')
    printf("it worked");
    getchar() returns a single character, not a string.
    Either call getchar twice (and use "char input" instead of "char *input") or use getline.
    Consider this post signed

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    How would I use getline in this situation?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Considering that there is no "getline", you wouldn't.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    Quote Originally Posted by serg_yegi View Post
    Well suppose I don't know how long the input is going to be. I can't specify a specific array size in that case, can I?
    You can suppose an input that is less than, let's say 1000 byte, so you allocate memory for a new char* array with malloc and then fill it with fgets() and at each iterative step you check whether the string that has been entered is equal to "In" or not, you could use strcmp() to do this

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, 05:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21