Various errors with pointers

This is a discussion on Various errors with pointers within the C Programming forums, part of the General Programming Boards category; I'm trying to do a program that randomly generates 12 digits, not equal to 9, using pointers. But I get ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    16

    Various errors with pointers

    I'm trying to do a program that randomly generates 12 digits, not equal to 9, using pointers. But I get some pretty nasty errors and I just can't understand what's wrong. I'm using XCode on Mac OS X.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    
    void random(int * pnt1, int * pnt2, int * pnt3, int * pnt4, int * pnt5, int * pnt6, int * pnt7, int * pnt8, int * pnt9, int * pnt10, int * pnt11, int * pnt12);
    
    int main()
    {
    	int a,b,c,d,e,f,g,h,i,j,k,l;
    	a=b=c=d=e=f=g=h=i=j=k=l=9;
    	
    	random(&a,&b,&c,&d,&e,&f,&g,&h,&i,&j,&k,&l);
    	printf("&d&d&d&d&d&d&d&d&d&d&d&d",a,b,c,d,e,f,g,h,i,j,k,l);
    	getchar();
    	return 0;
    }
    
    
    void random(int * pnt1, int * pnt2, int * pnt3, int * pnt4, int * pnt5, int * pnt6, int * pnt7, int * pnt8, int * pnt9, int * pnt10, int * pnt11, int * pnt12)
    {
    	int rando;
    	int i;
    	
    	for(i=0;i<12;i++)
    	{
    		do
    		{
    			rando=rand();
    			rando=floor(rando/1000000000);
    		}
    		while(rando=9)
    		
    		
    			
    			if(i==0)
    			{
    				pnt1=&a;
    				*pnt1=rando;
    			}
    			else if(i==1)
    			{
    				pnt2=&b;
    				*pnt2=rando;
    			}
    			else if(i==2)
    			{
    				pnt3=&c;
    				*pnt3=rando;
    			}
    			else if(i==3)
    			{
    				pnt4=&d;
    				*pnt4=rando;
    			}
    			else if(i==4)
    			{
    				pnt5=&e;
    				*pnt5=rando;
    			}
    			else if(i==5)
    			{
    				pnt6=&f;
    				*pnt6=rando;
    			}
    			else if(i==6)
    			{
    				pnt7=&g;
    				*pnt7=rando;
    			}
    			else if(i==7)
    			{
    				pnt8=&h;
    				*pnt8=rando;
    			}
    			else if(i==8)
    			{
    				pnt9=&i;
    				*pnt9=rando;
    			}
    			else if(i==9)
    			{
    				pnt10=&j;
    				*pnt10=rando;
    			}
    			else if(i==10)
    			{
    				pnt11=&k;
    				*pnt11=rando;
    			}
    			else if(i==11)
    			{
    				pnt12=&l;
    				*pnt12=rando;
    			}
    		}	
    				
    	}}
    Sorry for the quite long "if" sequence, but I didn't want to try a switch right now.

    So the errors I get are :

    1- Conflicting types for 'random' for my function prototype and declaration
    2- Syntax error before the first 'if'
    3- Syntax error before every 'else if'
    4- From 'pnt3' and the rest, I get : 'pnt3' redeclared as different kind of symbol
    5- Syntax error before '}' token

    And the warnings:

    1-At my function definition, I get, for every pointer from 3 to 12, "previous definition of 'pntX' was here"
    2- From 'pnt3' to 'pnt12' after the *pntX=rando, I get, "type defaults to 'int' in declaration of 'pntX'"
    AND "initialization makes pointer from integer without a cast"

    So, what have I done wrong?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    What a mess! How about we start over after we learn about arrays.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    16
    Quote Originally Posted by citizen View Post
    What a mess! How about we start over after we learn about arrays.
    Yeah well, I know it would be better, but we just can't use Arrays, specification in the homework ^^' Else I would have done that :P Same thing for character chains, we can't use them.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    For 2 -- do whatever while () ;
    The punctuation is important. Also rando=9 is not what you want.
    Also a through l are not defined inside random. The fact that you don't get that warning worries me.
    And your printf statement is going to surprise you.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    16
    Quote Originally Posted by tabstop View Post
    For 2 -- do whatever while () ;
    The punctuation is important. Also rando=9 is not what you want.
    Also a through l are not defined inside random. The fact that you don't get that warning worries me.
    And your printf statement is going to surprise you.
    Wow, yes, that ; after the while indeed fixed a lot of things! Thx for that!

    The while (rando=9) is there so the random generation number continues until I get a number else then 9.

    For your third sentence, I don't get what you mean about the random function.

    EDIT: Ok I just realized that the first statement in every 'if' block was useless since my pointers were already there. Now, I only got 2 errors remaining, concerning my prototype and function declaration.

    I get "conflicting types for 'random'". What does that mean?
    Last edited by Covalent; 11-02-2008 at 11:30 AM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Covalent View Post

    The while (rando=9) is there so the random generation number continues until I get a number else then 9.
    You don't have to believe me -- you can wait until you get the program working. I promise you every number you get will be 9. (Well, of course; you are assigning 9 to the variable rando, after all.)
    For your third sentence, I don't get what you mean about the random function.

    EDIT: Ok I just realized that the first statement in every 'if' block was useless since my pointers were already there. Now, I only got 2 errors remaining, concerning my prototype and function declaration.

    I get "conflicting types for 'random'". What does that mean?
    You shouldn't get conflicting types for random based on what you have posted here. Does your source match what you posted?

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    When fixing errors, always start with the first error, the one that is earliest in the file. Often many of the others can be taken care of by fixing that first one.
    You aren't generating random numbers properly. It wont work at all on a 32-bit machine I epxect.
    Anyway I think you must have misunderstood what you're supposed to write. I don't think your teacher would mean for you to ridiculously write a psuedo-array like that. You can just generate digits one at a time and output each one immediately.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    16
    Quote Originally Posted by tabstop View Post
    You don't have to believe me -- you can wait until you get the program working. I promise you every number you get will be 9. (Well, of course; you are assigning 9 to the variable rando, after all.)
    But if I write while (rando!=9), wouldn't that make loop my function until I do get a 9?


    You shouldn't get conflicting types for random based on what you have posted here. Does your source match what you posted?
    Here is my code corrected.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    
    void random(int * pnt1, int * pnt2, int * pnt3, int * pnt4, int * pnt5, int * pnt6, int * pnt7, int * pnt8, int * pnt9, int * pnt10, int * pnt11, int * pnt12);
    
    int main()
    {
    	int a,b,c,d,e,f,g,h,i,j,k,l;
    	a=b=c=d=e=f=g=h=i=j=k=l=9;
    	
    	random(&a,&b,&c,&d,&e,&f,&g,&h,&i,&j,&k,&l);
    	printf("&d&d&d&d&d&d&d&d&d&d&d&d",a,b,c,d,e,f,g,h,i,j,k,l);
    	getchar();
    	return 0;
    }
    
    
    void random(int * pnt1, int * pnt2, int * pnt3, int * pnt4, int * pnt5, int * pnt6, int * pnt7, int * pnt8, int * pnt9, int * pnt10, int * pnt11, int * pnt12)
    {
    	int rando;
    	int i;
    	
    	for(i=0;i<12;i++)
    	{
    		do
    		{
    			rando=rand();
    			rando=floor(rando/1000000000);
    		}
    		while(rando==9);
    		
    		
    			
    			if(i==0)
    			{
    				
    				*pnt1=rando;
    			}
    			else if(i==1)
    			{
    			
    				*pnt2=rando;
    			}
    			else if(i==2)
    			{
    				
    				*pnt3=rando;
    			}
    			else if(i==3)
    			{
    				
    				*pnt4=rando;
    			}
    			else if(i==4)
    			{
    				
    				*pnt5=rando;
    			}
    			else if(i==5)
    			{
    				
    				*pnt6=rando;
    			}
    			else if(i==6)
    			{
    			
    				*pnt7=rando;
    			}
    			else if(i==7)
    			{
    				
    				*pnt8=rando;
    			}
    			else if(i==8)
    			{
    		
    				*pnt9=rando;
    			}
    			else if(i==9)
    			{
    			
    				*pnt10=rando;
    			}
    			else if(i==10)
    			{
    			
    				*pnt11=rando;
    			}
    			else if(i==11)
    			{
    			
    				*pnt12=rando;
    			}
    		}	
    				
    	}
    Anyway I think you must have misunderstood what you're supposed to write. I don't think your teacher would mean for you to ridiculously write a psuedo-array like that. You can just generate digits one at a time and output each one immediately.
    I wanted to do that with a single pointer, but I just don't know how to..

  9. #9
    Registered User
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    5
    Try rename your random function..

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    16
    Ok, I renamed and it's working, thank you!

    And I saw what you meant by "what you will get will be interesting", I corrected the &d in the printf for &#37;d.

    But now, I always get the same numbers, 001010011201, which means my random function isn't working at all.

    Anything I should look at?

  11. #11
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    Quote Originally Posted by Covalent View Post
    I get "conflicting types for 'random'". What does that mean?
    I'd guess that your system is providing a function called "random", so your declaration is conflicting with that. POSIX systems provide random() in stdlb.h, and other systems may be similar.

    You can either rename your function (the better idea) or tell your compiler to be a strictly C compiler. This is possible in gcc with the -std=c89 option. If you do this, your compiler won't be allowed to use the name random.

    Renaming is better, in my opinion, because there's probably a good chance you'll wind up wanting, at some point, to use POSIX features (or whatever your implementation provides) that will disappear if you're using plain C.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you mean that every time you run the program, you get the same answers, that's expected and perfectly normal. If you mean that each of your twelve numbers is the same number, then that's bad.

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    16
    Ok I just forgot to add srand(time(NULL)); in my declaration. Still, every 12 digit number I get only has 0-1-2, never something else...

  14. #14
    Registered User
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    5
    You have to seed your rand() function.. Put srand(time(NULL)) in your main function.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Covalent View Post
    Ok I just forgot to add srand(time(NULL)); in my declaration. Still, every 12 digit number I get only has 0-1-2, never something else...
    Well, since your MAX_RAND is probably 2147483647, yes you will never get anything bigger than 2. Did you mean &#37;10 instead of /1000000000?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  2. Errors with header files in OpenGL using VisualC++
    By wile_spice in forum Game Programming
    Replies: 3
    Last Post: 06-22-2006, 08:56 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Pointers, Classes, and Errors o my!
    By Scottc1988 in forum C++ Programming
    Replies: 12
    Last Post: 03-13-2003, 09:14 PM

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