Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
#include <bits/stdc++.h>
using namespace std;
struct {
struct {
struct {
char *OwO[12];
}iwi;
}uwu;
}owo;
int main() {
*owo.uwu.iwi.OwO = "What's this?";
printf("%s\n", *owo.uwu.iwi.OwO);
return 0;
}
Hi guys I don't know how this code actually work? Can anyone explain this to me?
Consider for example this declaration
struct {
char *OwO[12];
}iwi;
It at first declares an unnamed structure with one data member that has the type of an array with 12 elements of the type char *. And then it declares an object named iwi of the structure.
So to access the data member OwO of the object iwi you can use the expression
iwi.OwO
that returns lvalue of the array OwO.
If to apply the operator * to the expression then the array OwO is implicitly converted to pointer to its first element and has the type char **. Dereferencing the pointer we get the first element of the array of the type char *.
We can assign the element with a string literal as
*iwi.OwO = "What's this?";
That is the first element of the array that has the type char * now gets the address of the string literal.
Here is a demonstrative program
#include <stdio.h>
struct {
char *OwO[12];
} iwi;
int main(void)
{
*iwi.OwO = "What's this?";
printf( "%s\n", *iwi.OwO );
return 0;
}
Its output is
What's this?
In the original code this unnamed structure is included into two other unnamed structures
struct {
struct {
struct {
char *OwO[12];
}iwi;
}uwu;
}owo;
That is with have object owo of the unnamed outer structure that has data member uwu of the enclosed unnamed data structure that in turn has data member iwi of the most inner unnamed structure.
So to access the data member OwO we have to list all names of object
owo.uwu.iwi.OwO
So we have gotten an access to the most inner data member OwO. And now dereferncing the expression as it was shown in the demonstrative program above we initialize the first element of the array with the string literal "What's this?".
And in the same way we can output it using this full expression
printf("%s\n", *owo.uwu.iwi.OwO);
It's several nested unnamed struct types.
Here is the same thing using named types, and indexing instead of dereferencing.
struct Inner
{
char* OwO[12];
};
struct Middle
{
Inner iwi;
};
struct Outer
{
Middle uwu;
};
Outer owo;
int main() {
owo.uwu.iwi.OwO[0] = "What's this?";
printf("%s\n", owo.uwu.iwi.OwO[0]);
return 0;
}
Related
I was giving a company's coding interview test (on mettl.com) and this was the problem : -
Given an array of "n" integers, add "2" to every element of the array and return the array.
And this was their format of code (I cannot change their format, I can just write code inside the function. Also, I don't have to read the input, it is passed through function already and also no "main-function" is allowed).
Here is what the code looked like in C++:
#include<bits/stdc++.h>
using namespace std;
//Read only region starts, you cannot change the code here
//Assume following return types when writing code for this question
struct Result{
Result() : output(){};
int output1[100];
};
Result arrange(int input1, int input2[])
{
//Read only region end...now...you can write whatever you want
int n;
n=input1;
int i=0;
int a[n];
while(i<n)
{
a[i]=input2[i]+2;
i++;
}
//...now..I am super confused...how do I return the array 'a' to result structure??
//I have very less idea about structures and objects in C++
}
My answer is in array - 'a' but I don't know how do I return it to the structure (output1[100]) ?
The function is declared to return a Result struct. So the function needs to create an instance of that struct in order to return it. And since the struct already has an array in it, you don't need to create your own array, just fill in the one that already exists.
Try this:
#include <bits/stdc++.h>
using namespace std;
//Read only region starts, you cannot change the code here
//Assume following return types when writing code for this question
struct Result{
Result() : output1(){};
int output1[100];
};
Result arrange(int input1, int input2[])
{
//Read only region end...now...you can write whatever you want
Result res;
for(int i = 0; i < input1 && i < 100; ++i)
{
res.output1[i] = input2[i] + 2;
}
return res;
}
To just answer the question, make a struct object in the function (“Result R;”) and use it’s member output1 array to copy into instead of array “a” (“R.output1[i] = ...;”). So just delete “a” array and replace with the struct object’s output1. Then return that struct object.
Structure can be passed to function through its object therefore passing structure to function or passing structure object to function is same thing because structure object represents the structure. Like normal variable, structure variable(structure object) can be pass by value or by references / addresses.
This question already has answers here:
struct pointer function points to other function of other struct
(3 answers)
Closed 6 years ago.
I'm trying to point a structure function to another function of another structure,
Plase consider this:
// Main Structure:
typedef struct
{
int GetValA(int a)
{
return a * 2;
}
} x;
typedef struct
{
int(*HGetValA)(int); // Pointer function
} hookx;
// Then
int main()
{
x v1;
hookx* v2;
v2 = (hookx*)&v1; // or 0x0 memory address
// Now declaring pointer function
v2->HGetValA = (int(*)(int))&v1.GetValA; // Pointing to function of the main structure.
}
for me, this looks good, but at compile time gives me the error:
[Warning] converting from 'int (x::)(int)' to 'int ()(int)'
[-Wpmf-conversions]
The pointer points to a member from class/struct does NOT really means to a address, it just points as an offset from this.
So the pointer type out of the class/struct (like in your code, int ()(int)) is different from the inner (like, int (::)(int)).
You have to declare the pointer with a class/struct name that seem to be a scope (and it does).
How to interpret the following c++ declaration?
int (*(*x[2])())[3];
This is from the example present at Type-cppreference.
The interpretation of the sample declaration in the question is present in the Type-cppreference page linked.
int (*(*x[2])())[3]; // declaration of an array of 2 pointers to functions
// returning pointer to array of 3 int
So the actual question is not about that case in particular; but about how any C++ declaration shall be read.
You can infer all those details starting at the declared name x and moving clockwise respecting parenthesis. You will get to the description above:
x is a an array of dimension 2 of pointers to functions that return a pointer to an array of dimension 3 of ints.
Better explained here as the Clockwise/Spiral Rule: http://c-faq.com/decl/spiral.anderson.html
It is an array of two pointers to functions that return pointer to array of type int[3] and do not have parameters.
Here is a demonstrative program
int ( *f() )[3] { return new int[2][3]; }
int ( *g() )[3] { return new int[4][3]; }
int main()
{
int (*(*x[2])())[3] = { f, g };
for ( auto func : x ) delete []func();
return 0;
}
This question already has answers here:
Why can't I assign an array variable directly to another array variable with the '=' operator?
(5 answers)
Closed 9 years ago.
The question is very simple but I'm confused that why the struct is behaving like this as all of its members are by default public, have a look at following code
struct Student
{
char name[20];
}
int main()
{
Student s;
s.name="ahmed";
}
This code gives an error that name should be a modifiable lvalue.
But if I assign value in a loop char by char, then it works fine like this
s.name[0]='a';//works fine
Arrays in C are non-assignable and non-copy-initializable and in C++ this behavior was inherited. On the right-hand side arrays decay to pointers but in your case your copy-initialization is just not allowed.
If you try this:
{
char name[20];
name="ahmed";
}
you're actually copy-initializing the array - and that's disallowed.
The only exception is initializing them with a string literal
char c[] = "abc";
In your case you should rather use a function called strcpy to accomplish that:
{
char name[20];
strcpy(&name[0],"ahmed"); // Obviously make sure that there's enough space
}
It's worth noting that you can also use strings to accomplish this task:
#include<iostream>
#include<string>
using namespace std;
struct Student{
string name;
};
int main()
{
Student s;
s.name = "ahmed";
}
Structure is not a problem in your case. Arrays in C are not assignable.
Instead of s.name = "ahmed" , use strcpy(s.name,"ahmed")
name is an array, and you can't assign to an array name. Instead, you should use std::strcpy in cstring to copy the contents of the C-style string.
std::strcpy(s.name, "ahmed");
Note that the problem has nothing to do with the fact that name is part of a struct. You would have the same problem with a native char array.
You must use:
struct Student
{
char *name;
};
int main()
{
Student s;
s.name = "ahmed";
return 0;
}
I am using gcc version 4.3.3 on my Ubuntu (i686). I have written a stripped down test program to describe my lack of understanding and my problem. The program shall tell me the size of the struct, which I implemented. So I have a typedef struct for a Message and a little main to play around:
#include <stdio.h>
typedef struct {
int size;
enum {token=0x123456};
} Message;
int main(int argc, char * argv[])
{
Message m;
m.size = 30;
printf("sizeof(int): %d\n",sizeof(int));
printf("sizeof(0x123456): %d\n",sizeof(0x123456));
printf("sizeof(Message): %d\n",sizeof(Message));
printf("sizeof(m): %d\n",sizeof(m));
}
While compiling this source with gcc I get the following warning, which I don't understand:
$ gcc sizeof.c
sizeof.c:5: warning: declaration does not declare anything
Line 5 refers to the enum line. I want that token in every Message, that I create. What am I doing wrong? What do I have to change to get rid of that warning?
My main contains several calls of sizeof(). When I run the program, you can see in the output that the integer has the size of four, the hex number has the size of 4, but the typedef struct Message has the size of 4, too:
$ ./a.out
sizeof(int): 4
sizeof(0x123456): 4
sizeof(Message): 4
sizeof(m): 4
That is very confusing to me. Why has Message the size of 4, although it contains an integer and an integer within an enum, each with the size of 4. If the sizeof(Message) would be at least 8, it would be logical to me.
But why is it only 4? How do I get the real size in Bytes of my Message? Or is this really the real size? If so, why?
Is there a difference in getting the size of a Message between C and C++?
An enumeration doesn't actually need any space, it's just a way for the compiler to recognize a set of literal numbers by a name.
You are not declaring anything with:
enum {token=0x123456};
Your declaration is similar to:
typedef struct {
int size;
int;
} Message;
If you declare your struct like this:
typedef struct {
int size;
enum {token=0x123456} e;
} Message;
There will be two fields, but e will not be initialized to anything. You need to set it manually for every instance: message.e=token.
The correct way to achieve what you want is, to use constructors in C++:
struct Message {
int size;
int token;
Message() : token(0x123456) {};
};
Or non-static data member initializers in C++11:
struct Message {
int size;
int token=0x123456;
};
There is no way to initialize field in struct declaration in C.
Line 5 does not declare any variable that is of type enum. So the compiler does the only thing it can do: ignore it.
If you want to create a member of that type in the struct, write something like
enum {token=0x123456} thetoken;
But be aware that this field can only have one valid value, is that what you want?
Edit:
Oh, and to answer your other question: I can't see a difference in output when compiling as C or C++. But there is a difference between how how you should write struct definitions.
typedef struct {
int size;
enum YouShouldDeclareAName {token=0x123456};
} Message;
your enum is a subclass/subtype of your Message struct, therefore bounds to Class and not object. Like a namespace. You do not create any variable with it.
Change it to:
typedef struct {
int size;
enum YouShouldDeclareAName {token=0x123456} token;
//or
YouShouldDeclareAName token2;
} Message;
You've defined a constant Message::token that's shared between all objects. Since it's shared, it doesn't count towards the size of a single object.
As the others answers note, you've declared an enumerated type, you just happened to do it inside a structure instead of at global scope. There's nothing to store, so it uses no memory.
Now if you were to declare an instance of your enumeration in that structure...
typedef struct {
int size;
enum {token=0x123456} e;
} Message;
int main(int argc, char * argv[])
{
Message m;
m.size = 30;
printf("sizeof(m): %d\n",sizeof(m));
}
sizeof(m): 8
Press any key to continue . . .
LINE 5:
enum {token=0x123456};
This line doesn't define any enum variable, its a declaration, because of this your compiler complains about line 5 saying its only a declaration.
proper usage should be:
enum {xyz=5} enum_variable_name;
Only then the compiler will allocate space for this.
Just like class, function, enum, static menber doesn't store in the object space!