C++ lack of logic on pointer initialisation & use [closed] - c++

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 8 years ago.
Improve this question
This is not logic :
int *ptr = &otherInt;
When we do that, ptr gives adress of otherInt, *ptr gives otherInt.
But we write explicitly that (*ptr) equals adress *ptr = &otherInt !
Logically, *ptr should give the adress, and ptr the otherInt.
Don't you think ?

I think you got it all wrong. Consider this:
int *ptr;
int otherInt = 10;
ptr = &otherInt; // Notice this line.
The asterisk is a part of the declaration, not the assignment.

Related

Can someone tell me what is wrong with this statement? [closed]

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 2 years ago.
Improve this question
How is this statement
int* p = new int[10];
p[10] = 5;
delete[] p;
different with this statement ?
int* p = new int[10];
*p = 5;
delete[] p;
I wanna what is wrong with the first code. I'm kinda new to C++, so any explanation is appreciated. Thanks.
The behaviour of p[10] is undefined as the array has only 10 elements and the first element is at position 0. You can access p[0] (which is the same as *p) to p[9] inclusive.
In other words, arrays in C++ are zero-based. Cf. Fortran, for example, where they are one-based.

What is the difference between pointers to array of pointers? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I came across these two terms
Int (*q)[3][4] and. int q[ ][3][4].
What's the difference these two terms?
And one more question .
Char a[ ]="abcd";
Char *p="abv";
a="ghj";
p="ajk";
Printf("℅s℅s",a,p);
Why this would not compile?
It won't compile because of the line:
a = "ghi"
This assigns a const char* to a char* directly. You can use strcpy to copy the string intead:
strcpy(a, "ghi")
You will still have warnings at this point, because you have not declared p as a const. You can fix this like so:
const char* p = "abv"

2 value in one variable (const and const_cast) c++ [closed]

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 8 years ago.
Improve this question
void main() {
const int a = 10;
const int *b = &a;
int *c = const_cast <int*>(b);
*c = 5;
cout<<a<<" "<<*b<<" "<<*c<<endl; //10 5 5
cout<<&a<<" "<<b<<" "<<c<<endl; //same address
cout<<*(int*)&a<<" "<<*&a<<endl; //5 10
}
what makes type cast affected this?
where is the value stored?
The program has undefined behavior: with the const_cast<int*>(b) you remove the const qualifier from an object which actually is const and the assignment to that object may have arbitrary effect.
The observed effects indicate that the implementation replaced uses of a with its immutable value while it dereferences b to determine the value. It could have arbitrary other effect, too, though. For example, a segmentation fault when trying to write a write protected location could be a possible outcome. Well, anything can happen.

C++ Pointer type cast from class name [closed]

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 9 years ago.
Improve this question
How to cast a Pointer in C++ from class name in String?
Psuedocode:
int * ptr = something;
myStruct ptrstruct = (ClassFromString("myStruct") ptr);
// The class/struct name is passed in as String
Thank you
I'm not a c++ guru but I have two ideas that may help with brainstorming:
May the use of the registry pattern as described here would be of help: Instantiate class from name?
Secondly, following the registry pattern idea you could crate a function for casting e.g. MyClass something = registry.cast("MyClass", ptr);
I am not sure but this must work
Only Void pointer or boost can help, if it happens
thing * p = something; // pointer to object
void * pv = p; // pointer to void
thing * p2 = static_cast<thing *>(pv); // pointer to the same object
Maybe same kind of situation is while returning values from Threads
Overall reflection is not possible in c++.
Its just brainstroming.

Why do I need to const_cast when releasing memory for a const data [closed]

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 9 years ago.
Improve this question
While releasing memory of a const data, why do I need to const_cast it. What will be the result If I ignore it.
You don't. This is completely valid, standard conform, code:
int const * const a = new int(42);
delete a;
It sounds like you are using std::free from <cstdlib> to do this instead, which signature is
void free(void *);
In this case the implicit conversion from int const * const to void * fails because you can only implicit const-cast, not cast const away. You want the first version in C++ anyway.