Thread: Help with basic C syntax

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    6

    Help with basic C syntax

    What is the value of expression *(p+i) in function f() in the following code:

    Code:
    #include<stdio.h>
        float m[4][4]={{1.5,2.5,3.5},{3,4.5,5.5},{5,6.5,7.5}};
        
    void f(float *p, int m, int n)
    {
    int i, b1=0, b2, s=0;
    for (i=m, b2=b1; i<=n; s+=*p, i++)
    if (*(p+i))
    b1++;
    else
    ++b2;
    printf("%d %d %d %f", b1, b2, s,p[i]);
    }
    
    int main()
    {
    f(&m[1][2],1,9);
    return 0;
    }
    When I debugged the code, after first if-else statement, if statement is passed and else is executed.
    So, how the value of *(p+i) is 0?

    Thanks for replies.

  2. #2
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    So, how the value of *(p+i) is 0?

    That's because

    Code:
    float m[4][4]={{1.5, 2.5, 3.5},{3, 4.5, 5.5}, {5, 6.5, 7.5}};
    
    is the same as
    
    float m[4][4]={{1.5, 2.5, 3.5, 0.0}, {3, 4.5, 5.5, 0.0},{5, 6.5, 7.5, 0.0}, {0.0, 0.0, 0.0, 0.0,}};
    Remember, all the values you don't give an initial value in your array are set to 0;

    Cheers

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variadic Template Basic Syntax
    By Grumpulus in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2014, 12:39 PM
  2. Few basic syntax questions
    By MartinR in forum C Programming
    Replies: 11
    Last Post: 02-07-2014, 11:40 AM
  3. Basic pointer syntax
    By Mnemonist in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2010, 10:37 PM
  4. Basic syntax in C
    By heras in forum C Programming
    Replies: 6
    Last Post: 03-16-2008, 08:05 AM
  5. A basic syntax question
    By IdioticCreation in forum C++ Programming
    Replies: 18
    Last Post: 11-23-2006, 06:48 PM

Tags for this Thread