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

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"

Related

why do we need strcpy()? [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 6 years ago.
Improve this question
struct prac
{
int name[3];
char name1[12];
} b1, b2, c2;
main()
{
int i;
struct prac b2={1,2,3};
strcpy(c2.name1,"goodmorning");
printf("%s",c2.name1);
}
here to store the values in b2 array we just need to write b2={1,2,3} but if I want to store some values in the string c2 we need to call strcpy(), why it shows error if I write c2="goodmorning", rather than using strcpy()?
Because you can't assign to an array, only initialize it (which you do when you define the second b2 variable) or by copying to it (which you do with the strcpy call).

Modify a char* in 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
I am trying to modify a c string in C++.
void modify(char* s)
{
s[0] = 'a';
}
If I do this, there will be some undefined behavior and can't run.
Let's assume s[0] is valid. I know char* s is immutable. Is there any possibility that I can modify the s[0] in place, which means, without creating a new string. Do the modification on the original string.
I think you might be misunderstanding some other answers you have seen on the web.
It is only undefined behavior to modify a string constant, not any char*.
As long as you strdup the constant string into a non-constant string, you can make whatever changes you want with it, because it is now in a mutable region of memory.
#include <stdio.h>
#include <string.h>
void modString(char* changeMe) {
changeMe[0] = 'g';
}
int main(){
char* foo = strdup("food");
puts(foo);
modString(foo);
puts(foo);
free(foo);
}

What is an environment vector? [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
I am writing a short program that displays the argument count (argc), argument vector(argv[]), and environment vector. However, I am unsure how to display the "environment vector" or what it is.
The "environment" parameter, traditionally named envp, is a zero-terminated array of char*.
You can display it like this:
int main(int argc, char* argv[], char* envp[])
{
while (*envp)
{
std::cout << *envp << std::endl;
envp++;
}
}
It's not part of POSIX (or any other standard) but supported by many compilers.

C++ lack of logic on pointer initialisation & use [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
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.

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.