Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

why the output is not 14,6,6?

Ritesh KumarRitesh Kumar Member Posts: 1

include<stdio.h>

int x;
void Q(int z)
{
z+=x;
printf("%d",z);
}
void P(int y)
{
x=
y+2;
Q(x);
*y=x-1;
printf("%d",x);
}
void main()
{
int x=5;
P(&x);
printf("%d",x);
}

Comments

  • Tyson BlairTyson Blair Member Posts: 35

    this code should not compile correctly

    Risk Assessment

    Converting from pointer to integer or vice versa results in code that is not portable and may create unexpected pointers to invalid memory locations
    https://www.securecoding.cert.org/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer

  • Tyson BlairTyson Blair Member Posts: 35

    int x;
    void Q(int z)
    {
    z+=x; // This should be ok
    printf("%d",z); // This should be ok
    }
    void P(int y)
    {
    x=y+2;// This should be ok
    Q(x);// This should be ok
    *y=x-1; // tryes to assign int value x-1 to pointer value y of type int *
    printf("%d",x);// This should be ok
    }
    void main()
    {
    int x=5;// This should be ok
    P(&x); // passes pointer x to P() basiclly of type int *
    printf("%d",x);// This should be ok
    }

  • Tyson BlairTyson Blair Member Posts: 35

    *y=x-1; // should cause a compile error
    P(&x); // should cause a compiler error

  • Tyson BlairTyson Blair Member Posts: 35

    compile results from GCC

    test.c: In function ‘P’:
    test.c:17:3: error: invalid type argument of unary ‘*’ (have ‘int’)
    *y=x-1;
    ^
    test.c: In function ‘main’:
    test.c:24:5: warning: passing argument 1 of ‘P’ makes integer from pointer without a cast [-Wint-conversion]
    P(&x);
    ^
    test.c:12:6: note: expected ‘int’ but argument is of type ‘int *’
    void P(int y)
    ^

  • Tyson BlairTyson Blair Member Posts: 35

    This might help you understand what is going on with the code

    P-code
    def X = 0; // Global
    Q(Z):
    Z = Z + X; // So Z = Z, X = X
    Print(Z); // Z = Z + Global X This is only used once in code with X = 7 and Z = 7 So It prints 14
    End
    P(Y):
    X = Y + 2; // X = 5; Y = 5; So X = 7 Now X is global, So X = 7 Globally
    Q(X); // X = 7
    Y = X - 1; // So Y = 6;
    Print(X): // Prints X has not been modifyed agian so X is still 7 Should Print X=7
    End
    Main():
    def X = 5; // Local
    P(X);
    Print(X); X is not a global so it does not get modifyed it stays 5 even when global X is modifyed by functions/methods/procedures etc.
    End

  • Tyson BlairTyson Blair Member Posts: 35

    in the above example the results are 14, 7, 5 as it does not deal with references, I will post one that does deal with refrences.

  • Tyson BlairTyson Blair Member Posts: 35

    Prosudo code
    def X = 0; // Global
    Q(Z):
    Z = Z + X; // So Z = Z, X = X
    Print(Z); // Z = Z + Global X This is only used once in code with X = 7 and Z = 7 So It prints 14, is allways a value(7) not pointer or refrence
    End
    P(Y):
    X = Y + 2; // X = 5; Y = 5; So X = 7 Now X is global, So X = 7 Globally Global X gets modifyed here
    Q(X); // X = 7
    Y = X - 1; // So Y = 6; Y = Pointer to X from Main() So Main->X gets modifyed here to 6
    Print(X): // Prints X has not been modifyed agian so X is still 7 Should Print X=7 X is still the global and still 7 Here
    End
    Main():
    def X = 5; // Local
    P(X); The Local X gets Modifyed In P(X) so it is now X=6
    Print(X); X was passed as refrence/pointer and got modifed in P(X) so it is now X=6
    End

  • Tyson BlairTyson Blair Member Posts: 35

    Please let me know if you understand the information

  • B00B00 Member Posts: 111 ✭✭✭

    Please use

    code
    markup
    
  • Tyson BlairTyson Blair Member Posts: 35

    I have tried but it did not work at all. I just broke the code and made a mess of things. here can I find info on how to use?

Sign In or Register to comment.