Challenge 1


#1

Can we have an official answer for the first challenge.

I am not very good and I would like to know if I am ‘out off lunch’ or not?

Thank you.

this is my answer;

float i=1;
printf (“float memory is %zu\n”, sizeof(&i);
return ;

output;
float memory is 8


#2

[code]#include <stdio.h>

int main(int argc, const char * argv[])
{

printf("A float is %zu bytes\n", sizeof(float));


return 0;

}
[/code]


#3

[quote=“stian24”]Can we have an official answer for the first challenge.

I am not very good and I would like to know if I am ‘out off lunch’ or not?

Thank you.

this is my answer;

float i=1;
printf (“float memory is %zu\n”, sizeof(&i);
return ;

output;
float memory is 8[/quote]
Hello stian24.
When I did Challenge 1, I did the same as ttppdd.

On the top of page 68 you have the code to illustrate uses of sizeof() to find the size of a data type.

Like ttppdd I did the obvious and just used that line but substituted int with float in the brackets.

[code]#include <stdio.h>

int main(int argc, const char * argv[])
{
float f = 17.0; /* we assign the value 17.0 to the float variable f /
printf(“the value of f is %f.\n”, f); /
print out the value assigned to float f /
printf(“a float is %zu bytes.\n”, sizeof(float)); /
the sizeof() function returns a size value of the float (float) into the token %zu */
}[/code]

I got:[quote]the value of f is 17.000000.
a float is 4 bytes.[/quote]
What you did is interesting and I am trying to figure out exactly what you did here. I copied out your code and overlooking a few typo’s I got the same thing you did:[quote]float memory is 8[/quote]

This is what I think happened with your answer to Challenge 1.
Look at the code on the middle of page 68 again - Line 4 then line 9

int *addressOfI = &i; . . . printf("A pointer is %zu bytes\n", sizeof(addressOfI));

In line 4 we assigned the value of &i to addressOfI and in line 9 we printed out the sizeof(addressOfI) which is essentially the value of &i which is a lot like your line of:

Line 9 prints out:[quote]A pointer is 8 bytes[/quote]Just like your line.

I hope I didn’t over explain here and I hope I am correct in what I am saying to you. I’m a noobee like yourself.

I just wish some of the more knowledgable users of this forum like ibex10 reads this and either says yes or NO! You’re wrong! You’re misleading stain24!

[color=#000080]JR[/color]


#4

[quote]Can we have an official answer for the first challenge.

I am not very good and I would like to know if I am ‘out off lunch’ or not?

Thank you.

this is my answer;

float i=1; printf ("float memory is %zu\n", sizeof (&i); return ;
output;
float memory is 8[/quote]
What is 8 bytes long? a float or pointer to a float.

The expression sizeof (foo) yields the size of a float:

float foo = 0;
sizeof (foo)

The expression sizeof (pfoo) yields the size of a pointer to a float:

float * pfoo = 0;
sizeof (pfoo)

But the expression sizeof (&foo) is a bit tricky:

float foo = 0;
sizeof (&foo)

And therefore we need to read the language spec for the sizeof operator; however, it should yield either the size of a pointer-to-float variable or the size of the largest integer required to store an address in:


#5

Glad you could join this discussion ibex10.
I took your answer to stian24 and put it into a project for clarity sake.[code]#include <stdio.h>

int main(int argc, const char * argv[])
{
float foo = 0;
float *pfoo = 0;
printf(“the size of float foo is %lu.\n”, sizeof(foo));
printf(“the size of a pointer to a float is %lu.\n”, sizeof(pfoo));
printf(“the size of an address of a float is %lu.\n”, sizeof(&foo));

return 0;

}[/code]
the result is[quote]the size of float foo is 4.
the size of a pointer to a float is 8.
the size of an address of a float is 8.[/quote]

so it goes like this…
foo is a variable (a float in this case and its size is 4 bytes)
*foo is a pointer to a float (and its size is 8 bytes)
&foo is a address of a float (and its size is 8 bytes)

I’m not clear on *foo. Is it a pointer to a float or is it a pointer to an address of a float?

[color=#000040]JR[/color]


#6

You probably meant to use pfoo there, because you have declared foo as of type float, not as of type float *.

Now, pfoo is a pointer to a float and *pfoo is a float.

[quote]I’m not clear on [color=#FF0000]*foo[/color]. Is it a pointer to a float or is it a pointer to an address of a float?
[/quote]
It is an error to write [color=#FF0000]*foo[/color] because foo is only a float, not a pointer to a float.

ppfoo is the address of a pointer to a float:

float foo = 0; float *pfoo = &foo; float **ppfoo = &pfoo


#7

Sorry ibex10, I made a mistake. I should have gone over things looking for typos.

So in regards to the topic of addresses and pointers:

-when we have an * before something it is a pointer.

-when we have an & before something it is an address.

right?

[color=#004040]JR[/color]


#8

Yes, but to be more precise:

  • In a declaration, * indicates what follows is a pointer variable;
  • In an expression, * before an address expression is the dereference operator; and
  • & is the address-of operator.
typedef struct FooBar FooBar;
struct FooBar
{
    int foo;
    int bar;
};

int main (int argc, const char * argv[])
{
    FooBar fooBar;
    FooBar * pfooBar = &fooBar;
    
    // Modify the fooBar...
    // These two statements are equivalent
    pfooBar->foo = 2 * pfooBar->bar;
    (* pfooBar).foo = 2 * (* pfooBar).bar;

    // Wipe the long at address 0 clean
    *((long *)0) = 0;

    return 0;
}

#9

#include <stdio.h>

int main(int argc, const char * argv[])
{
float i = 7;
printf(“A float is %zu bytes\n”, sizeof(i));
return 0;
}

THE OUTPUT IS :-

A float is 4 bytes
Program ended with exit code: 0

COULD SOMEONE TELL ME IF I AM WRONG OR RIGHT?

PLEASE
THANKS IN ADVANCE.


#10

You are correct.