C coding

sakthi.eeswari's Avatar
Go4Expert Member
Hi,
I need the coding to add 10 numbers using function and array and by not using pointers.whethet it is possible to add the numbers without using pointers.i need a clear idea about array and functions.If any one knows plaz help me.
-Sakthi
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Simple
Code: C
int add(int arr[10])
{
    int res = 0;
    for(int i=0;i<10;i++)
        res += arr[i];
    return res;
}

int main()
{   
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    int x = add(arr);
    return 0;
}
0
sakthi.eeswari's Avatar
Go4Expert Member
Hi,

I need the coding without specifying the array size directly.that is u gave the array size as 10 na,for that we have to get the number from the user.
-Sakthi
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
No problem juts use the pointer instead of aaray and use malloc to allocate the pointer and use like an array.
0
sakthi.eeswari's Avatar
Go4Expert Member
Hi,

Thanks for replying the previous program.I need a C coding for another program
for finding the inverse of the matrix,sorting of the matrix,without using the pointers only by using the array.Please help me.
-Sakthi
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Please try to put it as a different thread as that will help even others to find the things easier.
0
sakthi.eeswari's Avatar
Go4Expert Member
hi

i have one doubt regarding Strings.for reading the input from the keyboard we are using & for all other data type xcept string.can i know the reason y we are not using & for string in the scanf function.

Sakthi
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
I didn't really get your question, kindly explain!
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Scanf needs a pointer to the variable. Consequently, the "address-of" operator (&) is applied to the name of the variable. A C-string is not a single "thing", but a collection of such things (chars). The name of the array is treated as a pointer to its first element. Since it's already a pointer, the address-of operator is not required.

As Shabbir mentions, please put separate questions in separate threads. It's a matter of organization and promotes searchability.

Last edited by DaWei; 27Dec2006 at 23:13..
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
LOL! I took shakthi's '&' in the post for a literal AND and not the address-of operator!