-4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()    
{    
struct stock     {     
    char symbol[5];     
    int quantity;    
    float price;    
};    
struct stock *invest;

/*Create structure in memory */
invest=(struct stock *)malloc(sizeof(struct stock));
if(invest==NULL)
{
    puts("Some kind of malloc() error");
    exit(1);
}
/*Assign structure data */
strcpy(invest->symbol,"GOOG");
invest->quantity=100;
invest->price=801.19;

/*Display database */
puts("Investment portfolio");
printf("Symbol\tShares\tPrice\tValue\n");
printf("%-6s\t%5d\t%.2f\t%%.2f\n",\
       invest->symbol,
       invest->quantity,
       invest->price,
       invest->quantity*invest->price);         /*  I dont understand this line */

       return(0);

}
  • In the final output

Symbol - GOOG
Shares -100
Price - 801.19
Value - %.2f

  • How is the final pointer reference at line33 leading to the output %.2f ?
    (i do understand that the %% is used to display a %]

  • Why exactly is memory reallocated in a program?

Suppose, If i were to add a realloc() function in the code for the invest pointer, how is it going to affect the program or make it better in terms of performance?
How does realloc() helps in 'freeing' the memory?

(Iam not able to quite understand the relation of realloc() with malloc())

code_int
  • 11
  • 2
  • 4
    *i do understand that the %% is used to display a %*. That is why `%%.2f` outputs `%.2f`. – Yu Hao Mar 10 '15 at 07:38
  • 2
    Also note: You seem to be compiling without warnings. `invest->price=801.19` should raise a narrowing warning. `801.19` is a `double` literal that gets stuffed into a `float` value (try `801.19f`). – dhke Mar 10 '15 at 07:39
  • I use `code::blocks`, in which i might need to change the settings for compiler.It sometimes does not shows warning for codes that are intended to show warnings for beginners.Little help? – code_int Mar 10 '15 at 07:45
  • 1
    Mandatory link about [casting the result of malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Now what I really would like to know is, who are all the imbeciles who keeps teaching beginners to do that? – Lundin Mar 10 '15 at 07:46

2 Answers2

0

%%.2f needs an extra % symbol to make the final .2f into a format rather than the string literal that's being displayed.

Secondly realloc is intended to resize a previously calloc-ed array in memory.

phil
  • 561
  • 3
  • 10
0

How is the final pointer reference at line33 leading to the output %.2f ?

Because %% causes printf to display a % sign and not treat the second % as the start of a format specifier. That leaves .2f which is just literal characters in printf terms to be printed verbatim.

Why exactly is memory reallocated in a program?

realloc is used to change the size of a previously malloc'd memory region whilst preserving as much of the data in it as possible. In your case you allocated enough memory to store one instance of struct stock. If you decided you wanted two instances, you could do this:

invest = realloc(sizeof(struct stock) * 2);

That is almost equivalent to

struct stock* newInvest = malloc(sizeof(struct stock) * 2);
memcpy(newInvest, invest, sizeof(struct stock));
free(invest);
invest = newInvest;

The difference is that realloc can check the original block of memory to see if it can just be extended which is considerably faster than allocating a new block, copying the data and freeing the old block.

I dont understand this line

invest->quantity*invest->price); 

It's simply an expression that multiplies the quantity and the price.

JeremyP
  • 84,577
  • 15
  • 123
  • 161