C coding

Discussion in 'C' started by sakthi.eeswari, Sep 22, 2006.

  1. sakthi.eeswari

    sakthi.eeswari New Member

    Joined:
    Sep 13, 2006
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Simple
    Code:
    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;
    }
     
  3. sakthi.eeswari

    sakthi.eeswari New Member

    Joined:
    Sep 13, 2006
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    No problem juts use the pointer instead of aaray and use malloc to allocate the pointer and use like an array.
     
  5. sakthi.eeswari

    sakthi.eeswari New Member

    Joined:
    Sep 13, 2006
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Please try to put it as a different thread as that will help even others to find the things easier.
     
  7. sakthi.eeswari

    sakthi.eeswari New Member

    Joined:
    Sep 13, 2006
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  8. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    I didn't really get your question, kindly explain!
     
  9. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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: Dec 27, 2006
  10. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    LOL! I took shakthi's '&' in the post for a literal AND and not the address-of operator!
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    :eek:
     
  12. sakthi.eeswari

    sakthi.eeswari New Member

    Joined:
    Sep 13, 2006
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    hi

    i meant the & as the address operator.my question s that for all other data type for reading the input we are using &(ampersan) in the scanf function,but while we are reading strings we are not using &.can i know the reason for that
    eg;

    scanf("%s",a);

    Sakthi
     
  13. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    n C, a string is stored as a null-terminated char array. This means that after the last truly usable char there is a null, hex 00, which is represented in C by '\0'. The subscripts used for the array start with zero (0). The following line declares a char array called str. C provides fifteen consecutive bytes of memory. N.B. Only the first fourteen bytes are usable for character storage, because one must be used for the string-terminating null.

    char str[15];

    The following is a representation of what would be in RAM, if the string "Hello, world!" is stored in this array.

    Characters: H e l l o , w o r l d !
    Hex values: 48 65 6C 6C 6F 2C 20 77 6F 71 6C 64 21 00
    Subscripts: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    The name of the array is treated as a pointer to the array. The subscript serves as the offset into the array, i.e., the number of bytes from the starting memory location of the array. Thus, both of the following will save the address of the 0th character in the pointer variable ptr.

    ptr = str;
    ptr = &str[0];


    As because the name of the array is treated as a pointer you do not need to use the address-of operator because the pointer is itself an address.
    I hope I was able to help you out.
     
  14. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Or, as mentioned in post #9,
    If you're not reading your responses, why should we write them?
     
  15. sakthi.eeswari

    sakthi.eeswari New Member

    Joined:
    Sep 13, 2006
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    hi dawei

    i post the thread b4 3 days.i think pradeep asked me that what i m going to xplain.thats y i post the thread once again.after posting that i got the reply.sterday itself i read that.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice