Thread: Problems with pointers

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    103

    Problems with pointers

    I am trying to write a math engine. So far, I'm still working on the addition portion of the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void whatOperation(void);
    void add(char[], char[]);
    int main (void) {
    	printf("Welcome to the NumberChopper!\n\n\n\n");
    	whatOperation();
    	return 0;
    }
    
    //find out which function to execute
    void whatOperation(void) {
    		char functionRead[1000];
    		char addFunction[1000]=" add";
    		
    		printf("Please enter the function with parameters: ");
    		scanf("%s", &functionRead);
    		if(strncmp(functionRead, addFunction, 4)==1) {
    			add(functionRead, addFunction);
    		}
    		else {
    			printf("Hey you! Don't fall asleep at the keyboard! \n");
    			printf("%s", addFunction);
    		}
    }
    
    //add function
    int countCharacters(char functionRead) {
    	int charactersInInput=0;
    	while(functionRead[charactersInInput]!="\n") {
    		charactersInInput++;
    	}
    	return charactersInInput++;
    	
    }
    void add (char functionRead[], char addFunction[]) {
    	printf("Add function successfully acessed.\n");
    	
    	//Pointers
    	char *functionReadPtr=functionRead+7;
    	int charactersInInput=0;
    	char *testPointer = functionRead;
    	char integers[1000];
    	
    	printf("Test pointer: %s \n", testPointer);
    	printf("Parameter pointer: %s \n", functionReadPtr );
    	
    	
    	
    	
    	
    	
    }
    When I build it with CodeLite I get:
    Code:
    C:/Documents and Settings/Dhaivat Pandya/Desktop/helloWorld.c/NumberChopper/NumberChopper.c: In function `countCharacters':
    C:/Documents and Settings/Dhaivat Pandya/Desktop/helloWorld.c/NumberChopper/NumberChopper.c:32: error: subscripted value is neither array nor pointer
    mingw32-make.exe[1]: *** [Debug/NumberChopper.o] Error 1
    mingw32-make.exe: *** [All] Error 2
    ----------Build Ended----------
    1 errors, 0 warnings

    I don't understand the problem

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Code:
    int countCharacters(char functionRead)
    It seems you are not passing an array or pointer to the function. Instead it is just a char.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Poincare View Post
    I am trying to write a math engine. So far, I'm still working on the addition portion of the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void whatOperation(void);
    void add(char[], char[]);
    int main (void) {
    	printf("Welcome to the NumberChopper!\n\n\n\n");
    	whatOperation();
    	return 0;
    }
    
    //find out which function to execute
    void whatOperation(void) {
    		char functionRead[1000];
    		char addFunction[1000]=" add";
    		
    		printf("Please enter the function with parameters: ");
    		scanf("%s", &functionRead);
    		if(strncmp(functionRead, addFunction, 4)==1) {
    			add(functionRead, addFunction);
    		}
    		else {
    			printf("Hey you! Don't fall asleep at the keyboard! \n");
    			printf("%s", addFunction);
    		}
    }
    
    //add function
    int countCharacters(char functionRead) {
    	int charactersInInput=0;
    	while(functionRead[charactersInInput]!="\n") {
    		charactersInInput++;
    	}
    	return charactersInInput++;
    	
    }
    void add (char functionRead[], char addFunction[]) {
    	printf("Add function successfully acessed.\n");
    	
    	//Pointers
    	char *functionReadPtr=functionRead+7;
    	int charactersInInput=0;
    	char *testPointer = functionRead;
    	char integers[1000];
    	
    	printf("Test pointer: %s \n", testPointer);
    	printf("Parameter pointer: %s \n", functionReadPtr );
    	
    	
    	
    	
    	
    	
    }
    When I build it with CodeLite I get:
    Code:
    C:/Documents and Settings/Dhaivat Pandya/Desktop/helloWorld.c/NumberChopper/NumberChopper.c: In function `countCharacters':
    C:/Documents and Settings/Dhaivat Pandya/Desktop/helloWorld.c/NumberChopper/NumberChopper.c:32: error: subscripted value is neither array nor pointer
    mingw32-make.exe[1]: *** [Debug/NumberChopper.o] Error 1
    mingw32-make.exe: *** [All] Error 2
    ----------Build Ended----------
    1 errors, 0 warnings

    I don't understand the problem
    i think '&' should be removed from the red portion.coz while inputting a string using scanf '&' is not used. correct me if i'm wrong.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Code:
        if(strncmp(functionRead, addFunction, 4)==1) {
    use > 0 for check, because it is not guaranteed by the standard, that it will be only 1

    about your problem
    Code:
    int countCharacters(char functionRead) {
    it is an array, it must be char functionRead[] or char *functionRead
    Oh, it was said
    Last edited by c.user; 04-26-2009 at 06:22 AM.

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. problems passing pointers
    By doormat in forum C Programming
    Replies: 9
    Last Post: 04-11-2004, 04:38 PM
  3. Class Pointers
    By ventolin in forum C++ Programming
    Replies: 8
    Last Post: 04-04-2004, 06:07 PM
  4. Array of Pointers + Deleting An Object = Problems
    By Nereus in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 12:16 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM