|
It is a global variable with file scope.
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
It is not "global"; You are just confusing the issue by saying that.
|
|
|
|
|
what is the use of "%%" meaning in c language? and where will we use?give me a small exapmle?
|
|
|
|
|
Used with printf() if you want to print a percent sign.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
The code below uses a variable's type as size in malloc(). And I printf the size of //line 1, b and c to find out their sizes malloc will allocation the space for them in heap.
Question part 1:
One person told me I should use // line a's size to malloc in // #####, while the other person said I should use //line c instead. They do have the same size in printf(). Which one is correct? Why?
Question part 2:
// line b and // line c both have the same type of variable pointers, but why do they have different sizes?
struct Vector {
double *data;
size_t size;
};
int main()
{ int a, b, d;
int sz = 12;
a = sizeof(struct Vector); // line a
printf("%d\n", a); //a = 16
b = sizeof(struct Vector*); // line b
printf("%d\n", b); //b = 8
c = sizeof(*retVal); // line c
printf("%d\n\n", c); //c = 16
struct Vector *retVal = malloc (sizeof (struct Vector)); //#####
retVal->data = malloc (sz * sizeof (double));
// Set size and return.
retVal->size = sz;
printf("retVal->size: %d\n", sz);
return 0;
}
Thank you for your help. 
|
|
|
|
|
Both are correct. sizeof(struct Vector) expands to the size of the type while sizeof(*retVal) expands to the size of the type pointed to by the variable.
The second method should be preferred because it will be always the correct size even when changing the variable type. Imagine that you have another structure with different size. When you now change the type of retVal to that structure, you must also change the type for the first sizeof() but not for the second.
|
|
|
|
|
Thanks for your response. Just to make sure I understand your answer, I listed .....I'm thinking the second malloc() used in my previous example can be replaced by the line below?
sizeof(12 * sizeof(retVal->data));
Thank you!
|
|
|
|
|
No, that is wrong. The second example from your initial post (sizeof(struct Vector*) ) returns the size of a pointer which is 4 or 8 with 32-bit or 64-bit builds.
The sizeof[^] operator expands to the size of the argument during compilation. If the argument is not a type but a variable, the type of the variable is returned. If the variable is a pointer, the pointer must be dereferenced using the * operator to get the size of the type instead the size of the pointer. Similar applies when the type is a pointer itself.
But when passing a type followed by * like in your second example, it is a pointer to the type. In that case the type does not care because all pointers have the same size.
Maybe you are irritated how the * operator is treated. With declarations it indicates a pointer. When accessing variables, it accesses the content of pointer variables.
|
|
|
|
|
Thank you for clarifying my confusion, Jochen!! I appreciate it.
|
|
|
|
|
Jochen provided a more complete answer.
modified 21-Nov-15 6:54am.
|
|
|
|
|
I have a single doc multiple views app in a tabbed view type mainframe. I have printing working nicely for each view on its own, but I need to print all views in one 'report'. I started by adding a "Print All" command in CMainFrame , and then traversing all document views (as CView pointers), but it gets very messy for a few reasons:
- each view must be cast to its RUNTIME_CLASS and checked to find which it is, so that its OnPrint function can be called
- each view has different scaling, so a single CPrintInfo won't suffice
- its not known in advance how many pages to set, but this can be overcome with m_bContinue checks.
Any suggestions on how to print a single document multiple views please?
Thanks!
|
|
|
|
|
.
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
modified 20-Nov-15 8:46am.
|
|
|
|
|
|
Brisingr Aerowing wrote: WHY ON EARTH IS IT MISSING IN THE FIRST PLACE?!? That is the proper question.
Could you please report the exact error message (and any accessory, useful info)?
|
|
|
|
|
Brisingr Aerowing wrote: the ~ operator (bitwise not) not being implemented That would break a lot of software, I suspect something else is the actual issue. A sample of the code and the error details would help.
[edit]
Are you sure this is MSVC 14? no such version referenced on MSDN[^] as far as I can see.
[/edit]
|
|
|
|
|
Richard MacCutchan wrote: Are you sure this is MSVC 14? That is the compiler version: Microsoft Visual C/C++ Compiler version 14. That version is part of Visual Studio 2015.
|
|
|
|
|
MSVC is the IDE not the compiler.
|
|
|
|
|
You are right, it is not only the compiler but the complete C/C++ development environment.
But version 14 is those from VS 2015. See for example the Wikipedia[^].
|
|
|
|
|
I have VS 2013 express and the C compiler is:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC>cl -v
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
|
|
|
|
|
The 18 is the RTM number from my above link.
What a mess with all these different numbers for a single product 
|
|
|
|
|
OP deleted his question; I can guess why.
|
|
|
|
|
void assign(size_type n, el const T& el = T())
This function is a member function of the STL list class and I do not understand the bolded part. I understand the const T& el is a reference to a constant parameter which means I cannot change the value/content of the passed argument + I understand that el = T() means a default parameter so if there is no argument passed in the calling function the el will use the no-argument constructor of the T() type OK. But what make me confused the el at the beginning before the const?!!! so if someone can fill the blank gaps for me I will be grateful . Thanks in advance.
|
|
|
|
|
|
I read it in a book called "Data Structures and Algorithms in C++, 4th Edition by Adam Drozdek" it was in chapter 3 "Linked Lists" at the beginning of page 110 . It made me had the feeling of this when read it, and after searching and find nothing I become like this ;P;P (i.e., psycho). Thanks by the way for reply 
|
|
|
|
|
Looks like a typo, to me.
|
|
|
|