Thread: sizeof help

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    63

    Loop through array help

    NEW QUESTION:
    How can I get it to loop through an array until it reaches the value of -1? Can I do
    while(i!=-1)? I think a for statement afterwards but what do I put in the middle? for(j=0,?,j++)?

    DISREGARD:
    I have to get the number of elements in an array using a function. This is what I have, and I have tried many variations and changing things around but I cannot get it to return the correct number. Point me in the right direction please?

    I only attached the parts that are relevant to my question.

    Code:
    printf("Number of elements is %d in Set 1 is %d", setCardinality(a)); /*pass array a to function setCardinality*/
    
    setCardinality(int a[]){
    	
    	int numberElements;
    
    	numberElements=((sizeof(a))/(sizeof(int)));
    	
    	return (numberElements);
    
    }
    Last edited by saahmed; 03-08-2006 at 06:38 PM.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    right when u send an array to a fucntion u are sending the base address of an array. this type of fucntion is called call my reference. so the fucntion formal parameter is just a int pointer rather than a actaull array. so when get sizof that parameter it just gives u the size of an int pointer that is 4 not the sizeof an array.

    Code:
    #include<stdio.h>
    
    int setCardinality(int a[],int n){
    	
    	int numberElements;
    
    	numberElements=((sizeof a)*n/(sizeof(int)));
    	
    	return (numberElements);
    
    }
    int main()
    {
        int a[7];
        printf("The size of an array is %d",setCardinality(a,7));
        getchar();
        return 0;
    }
    /*my output
    The size of an array is 7
    */
    ssharish2005

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by ssharish2005
    right when u send an array to a fucntion u are sending the base address of an array. this type of fucntion is called call my reference. so the fucntion formal parameter is just a int pointer rather than a actaull array. so when get sizof that parameter it just gives u the size of an int pointer that is 4 not the sizeof an array.

    Code:
    #include<stdio.h>
    
    int setCardinality(int a[],int n){
    	
    	int numberElements;
    
    	numberElements=((sizeof a)*n/(sizeof(int)));
    	
    	return (numberElements);
    
    }
    int main()
    {
        int a[7];
        printf("The size of an array is %d",setCardinality(a,7));
        getchar();
        return 0;
    }
    /*my output
    The size of an array is 7
    */
    ssharish2005
    yes, I just figured this out. I guess I dont need to use sizeof, I figured since it was in the chapter we are covering, I might need to use it in the homework. But, it was not a requirment, so I can make do without it. Just starting to learn pointers. Thanks for the help though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  4. Model not showing up on screen...
    By Shamino in forum Game Programming
    Replies: 14
    Last Post: 03-09-2006, 08:00 PM
  5. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM