Mystery number! noob

This is a discussion on Mystery number! noob within the C Programming forums, part of the General Programming Boards category; When I run it, it lists the row but some number appears at the end! From where, why? Code: #include ...

  1. #1
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78

    Mystery number! noob

    When I run it, it lists the row but some number appears at the end! From where, why?

    Code:
    #include <stdio.h>
    
    void main()
    
    {
    	int sranje;
    	int a,rez, n=5;
    	int polje[100];
    
    	for(a=1;a<=n;a++)
    	{
    		printf("Unesite %d. polje: ", a);
    		scanf("%d", &polje[a]);
    	}
    
    	rez = funk(polje, n);
    	printf("%d", rez);
    	
    	scanf("%d", &sranje);
    }
    
    int funk(int polje[100],int n)
    {
    	int a;
    	for(a=n;a>0;a--)
    	{
    printf("\n%d", polje[a]);
    	}
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    * Don't use void main(), see the FAQ http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    * Arrays in C are 0-based, not 1 based, ie they start at 0.
    * Avoid sscanf, perhaps flush the input before you ask for a number (use fgets and sscanf). See http://faq.cprogramming.com/cgi-bin/...&id=1043284385 and http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    You seem to start at an index of 1 when you get input, yet you wind back to 0 when you print it.
    Last edited by zacs7; 08-18-2007 at 07:43 AM.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You are not returnning anything from Funk() so printing out 'rez' is just going to display whatever random crap was left in that part of memory.

  4. #4
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    but i cant return array, can I?I would need to store array in some other variable?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by shardin View Post
    but i cant return array, can I?I would need to store array in some other variable?
    No, I think what mike_g was saying is that you are not returning anything from the function, but you are printing a variable that is set to the return value FROM the function - this return value will be some "rubbish" that is essentially whatever happens to be in the register that is used for passing return values - could be almost any value (and it's most likely your mystery number).

    --
    Mats

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    How does that even compile? It should give you an error about funk() not having a return statement shouldn't it?

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Depends on what the flags he invoked the compiler with. But it should at least give him a warning, even on the lowest warning level.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    How does that even compile? It should give you an error about funk() not having a return statement shouldn't it?
    Most compilers only warn, not error for "missing return" paths, but I agree that having some warnings enabled would be a good thing for ALL users of compilers.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  2. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  3. Finding a number within a number
    By jeev2005 in forum C Programming
    Replies: 2
    Last Post: 01-10-2006, 07:57 PM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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