Thread: pointer issues

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    12

    pointer issues

    hey guys, i'm pretty new to C, worked with java before.
    I thought those two languages are very similar, but the pointers are killing me:

    i have to write a c-program which recognizes 5 different operations und executes them.
    add, sub, mul, gcd and isprime.

    ie: input string "12*6" should result in "72".

    its working fine for add,sub and mul, but there is a conflict between gcd and prime.

    Like i told, im pretty new to C, dont laught at me if the code is that bad

    Sorry for the monster box, i dont know how to create a scroll box

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    long int isnumcheck (char *string)
    {	
    	char res[20]; 
    	
    	char *rest=res;
    	
    		
    	strtol(string, &rest, 0);
    	
    	if (strcmp(rest, "")==0)
    		
    	{	
    		return 1;
    
    	}
    	
    	else 
    		
    		return 0;
    }
    
    
    
    int ggt (int a, int b) {
    	
    	if (a == 0) return b;
    	
        else while (b != 0) {
            if (a > b) 
    		{
    			a = a - b;
    		}
            else b = b - a;
    	}
    	return a;
    	
    }
    
    int isprime (int n) {
    	
    	for(int i=2;i<=(n/2);i++)
    		if(n%i==0) return 0;
    	
    	return 1 ;
    }
    
    
    int arithmexe (char* zahl1, char* zahl2, int operation)
    // operation 0 = PLUS ; 1 = MINUS ; 2 = MAL ; 3 = Primzahl; 4 = GGTeiler
    
    
    {
    	int erste=atoi(zahl1);
    	int	zweite=atoi(zahl2);
    	int ausgabe;
    		
    	if (operation==0)
    	{
    		ausgabe=erste + zweite;
    		
    	}
    	if (operation==1) 
    	{
    		ausgabe=erste-zweite;
    	}
    	if (operation==2) 
    	{
    		ausgabe=erste*zweite;
    	}
    	if (operation==3)
    	{
    		ausgabe= isprime(*zahl1);
    	}
    	if (operation==4) {
    		ausgabe==ggt(*zahl1, *zahl2);
    	}
    	return ausgabe;
    
    
    }
    
    //MAIN
    
    int main()
    {
    	
    	char befehl[20]="", plus[20]="", minus[20]="", mal[30]="", klammer1[20]=""; //
    	int		eins=0, zwei=0;
    	char  *plustok=0,*minustok=0, *maltok, *ggttok=0, *primtok=0, *plustokh=0, *minustokh=0, *maltokh=0, *ggttokh=0, *klammertok1=0, *klammertok2=0;
    	
    	
    	printf("Folgendes Eingabeformat erforderlich:\n");
    	printf("a+b / a-b / a*b / ggt(a,b) / isprim(a)\n");
    	printf("Zum Beenden: q!\n");
    	printf ("Befehl eingeben: \n");
    	scanf ("%s", befehl);
    	printf("Eingabe: %s\n", befehl);
    	
    	
    	
    // LOOP	
    	
    		
    	while (strcmp(befehl, "q!")) {
    	
    		
    		strcpy(plus,befehl);
    		strcpy(minus, befehl);
    		strcpy(mal, befehl);
    		strcpy(klammer1, befehl);
    
    		plustok=plus;
    		minustok=minus;
    		maltok=mal;
    		klammertok1=klammer1;
    		
    	
    		plustok		=	strtok(plus, "+");
    		plustokh	=	strtok(NULL, " ");
    		minustok	=	strtok(minus, "-");
    		minustokh	=	strtok(NULL, " ");
    		maltok		=	strtok(mal, "*");
    		maltokh		=	strtok(NULL, " ");
    		/*
    		klammertok1	=	strtok(klammer1, "(");
    		klammertok1	=	strtok(NULL, " ");
    		eins		=	strcmp(klammer1, "ggt");
    		zwei		=	strcmp(klammer1, "isprim");
    
    		
    	
    
    		if (eins==0) 
    			{	
    				ggttok		=	strtok(klammertok1, ",");
    				ggttokh		=	strtok(NULL, ")");	
    			}
    		if (zwei==0) 
    			{
    				primtok		=	strtok(klammertok1, ")");
    			}
    		
    		
    		
    		 printf ("plus: %s\n", plustok);
    		 printf ("plus2: %s\n", plustokh);
    		 printf ("Minus: %s\n", minustok);
    		 printf ("Minus2: %s\n", minustokh);
    		 printf ("mal: %s\n", maltok);
    		 printf ("mal2: %s\n", maltokh);
    		 printf ("klammer: %s\n", klammertok1);
    		 printf ("prim: %s\n", primtok);
    		 printf ("ggt: %s\n", ggttok);
    		 printf ("ggt2: %s\n", ggttokh);
    		*/
    		
    		
    		if (isnumcheck(plustok) && isnumcheck(plustokh))
    			
    			{	
    				printf("%d\n",arithmexe(plustok, plustokh, 0));
    
    			}
    		else if (isnumcheck(minustok) && isnumcheck(minustokh))
    				{	
    					printf("%d\n",arithmexe(minustok, minustokh, 1));
    				}
    		else if (isnumcheck(maltok) && isnumcheck(maltokh))
    				{
    					printf("%d\n", arithmexe(maltok, maltokh, 2));
    				}
    				
    		/*else if (isnumcheck(primtok)) 
    			{
    					printf(" %d\n",arithmexe(primtok,"1",3));
    				printf ("plus: %s\n", plustok);
    				printf ("plus2: %s\n", plustokh);
    				printf ("Minus: %s\n", minustok);
    				printf ("Minus2: %s\n", minustokh);
    				printf ("mal: %s\n", maltok);
    				printf ("mal2: %s\n", maltokh);
    				printf ("klammer: %s\n", klammertok2);
    				printf ("prim: %s\n", primtok);
    				printf ("ggt: %s\n", ggttok);
    				printf ("ggt2: %s\n", ggttokh);
    			}
    	
    		else if (isnumcheck(ggttok) && isnumcheck(ggttokh))
    			{
    				
    				printf("%d\n",arithmexe(ggttok, ggttokh, 4));
    			}
    		*/
    		else {
    			
    			printf("falsche Eingabe\n");
    		}
    
    			
    			
    		
    		maltok		=	0;
    		maltokh		=	0;
    		eins		=	0;
    		zwei		=	0;
    		klammertok2	=	0;
    		klammertok1	=	0;
    		ggttok		=	0;
    		ggttokh		=	0;
    		primtok		=	0;
    		
    		
    		printf("Befehl eingeben\n");
    		scanf ("%s", befehl);
    		printf("Eingabe: %s\n", befehl);
    
    
    	} // LOOPEND
    
    }
    Last edited by Bobbel; 08-14-2010 at 12:02 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What conflict? nuclear , human rights ?
    Last edited by Bayint Naung; 08-14-2010 at 12:06 PM. Reason: added more info.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    12
    Is someone able to help me?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'll look into it. In the meantime, here are some things:
    Code:
    	if (operation==4) {
    		ausgabe==ggt(*zahl1, *zahl2);
    	}
    You probably meant assignment.
    Also consider reading SourceForge.net: Scanf woes - cpwiki

    EDIT:
    The code is pretty confusing as it is now, with all german (I believe) names and whatnot all over the place and a naive approach to dealing with this.
    If you have time, I would suggest another approach usually used by things like calculators.
    Last edited by Elysia; 08-14-2010 at 03:00 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    12
    ohh you're right! thx, one issue solved

    edit:
    any tips, how making it less "naive"?
    Last edited by Bobbel; 08-14-2010 at 03:05 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The common approach to a calculator is reading an identifier, interpreting it, storing it, then moving on.
    Say, you're going to do a calculation. Read a number, push it onto a stack.
    Read operation. Store it.
    Read the next number. Pop the number off the stack. Perform the operation and push the new result onto the stack.
    If it's a function, then it's just a little different order. First read and interpret function, then read arguments and perform action.

    Anyway, regarding your code, I believe I've figured out everything.
    First off:
    else if (isnumcheck(primtok))
    The problem here is that unless you actually type isprim, primtok will be NULL and isnumcheck never checks for NULL.
    So either:
    • Initialize primtok to a valid string.
    • Make isnumcheck check for NULL and fail if it is.


    Then, in your operation function, you are passing the first character of the passed string into the operation functions. You must understand that's simply not going to work. One character is not a number. Furthermore, a number can take up more than one character.
    You want to pass in the actual numbers that you've extracted:

    Code:
    	if (operation==3)
    	{
    		ausgabe= isprime(erste);
    	}
    	if (operation==4) {
    		ausgabe=ggt(erste, zweite);
    	}
    And then there was the comparison instead of the assignment bug.

    You need to learn to use a debugger. These were very simple issues that could have been found with just a small amount of experience instead of evil printf statements.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    12
    my "isnumcheck" function does check if the string is NULL, it differs between 0 and NULL, and fails if string=NULL, because "rest" is in that case not empty.


    Then, in your operation function, you are passing the first character of the passed string into the operation functions. You must understand that's simply not going to work. One character is not a number. Furthermore, a number can take up more than one character.
    I didn't understand this, but if I leave out ggt prime works fine and vice versa.

    im trying to work with the debugger, but does show up anything, unless i try ggt, than it crashes:
    Last edited by Bobbel; 08-14-2010 at 04:10 PM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Bobbel View Post
    my "isnumcheck" function does check if the string is NULL, it differs between 0 and NULL, and fails if string=NULL, because "rest" is in that case not empty.
    No, it does not. It invokes undefined behavior by trying to pass it off to strtol which expects a string and not a NULL pointer.
    And I don't see what you mean by "it differs between 0 and NULL".

    I didn't understand this, but if I leave out ggt prime works fine and vice versa.
    Take a look at the strings
    "100" and "200"
    The pointers point to the first character in the strings, '1' and '2' respectively.
    By dereferencing the pointers, you get the first character, '1' and '2' respectively.
    Now, a character is basically an integer. All characters are represented as numbers and simply "mapped" to some graphic when they're printed.
    So now you pass '1' and '2' to the functions, which is not the same as 1 and 2. Furthermore, this is not 100 and 200 which the real numbers were.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    12
    okay understand this, but aint i converting the whole char-array, with atoi()

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but you aren't passing those converted numbers to the functions. Also better to use strtol.
    Be sure to fix scanf too (see link).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    12
    yeah i changed to scanf_s

    But how do i fix isnumcheck to check for Null, which would be enough or?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    scanf_s isn't enough; see my link.
    Code:
    	if (string == NULL)
    		return 0;
    Checking to make sure string isn't NULL should be enough.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Aug 2010
    Posts
    12
    but doesnt
    Code:
    	if (strcmp(rest, "")==0)
    
    	{
    		return 1;
    
    	}
    
    	else
    
    		return 0;
    do the same?

    Yes, but you aren't passing those converted numbers to the functions.
    Why not?

    edit:

    thanks a lot for your help
    Last edited by Bobbel; 08-14-2010 at 04:31 PM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Bobbel View Post
    but doesnt
    Code:
    	if (strcmp(rest, "")==0)
    	{
    		return 1;
    	}
    	else
    		return 0;
    do the same?
    Absolutely not. NULL is not a string!

    Why not?
    Code:
    ggt(*zahl1, *zahl2);
    What does this do, do you think?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Posts
    12
    sends the pointers of the integers to the function

    edit:
    lol now i see it, hell im wondering why it even worked.

    i picked the strings, instead of the integers.

    But ggt still doesn't work!
    Last edited by Bobbel; 08-14-2010 at 04:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About pointer to function (Soved by myself)
    By hchingwong in forum C Programming
    Replies: 5
    Last Post: 08-06-2010, 03:47 AM
  2. I hate pointers or Pointer Issues
    By bolivartech in forum C Programming
    Replies: 9
    Last Post: 11-14-2009, 11:48 AM
  3. Strncat and Realloc - pointer issues
    By bhenderson in forum C Programming
    Replies: 6
    Last Post: 08-16-2009, 01:59 PM
  4. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM