Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The question is in the title:
What is int * a[][10];
Is it an array of pointers to arrays of int? I tried to youse the clockwise/spiral rule but I am not sure...
int * a[][10];
is an illegal declaration in C++, as the storage size isn't known. You need to initialize it with arrays of 10 pointers to int, like so:
int* a[][10] = {{nullptr}}; // initialize with one array, the latter consisting of null pointers
or, even simpler,
int* a[][10] = {{}};
Once initialized, it becomes an array of arrays-10 of pointers to int.
int *a[][10], is to be read as pointer to 2D array of undefined rows (by default I guess it is 0) and 10 columns,with data read out row-wise in memory layout.
But this declaration is only possible in C.
If we declare a 2D array namely int abc[10][10] then we can make 'a' to point this array 'abc'.
Related
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 1 year ago.
Improve this question
So I had this question at an exam. And I don't know which answer is correct.
#include<bits/stdc++.h>
using namespace std;
struct Test{
int note;
char exam[20];
}qwerty;
int main()
{
cout<<qwerty.exam;
}
What is the type of qwerty.exam in the previous code? Is it char or char[20]?
They said the right answer is char[20]. But if the type is char[20], why can't I declare something like: char[20] exam; ?
Moreover, I found this on cplusplus website:
Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:
type name [elements];
where type is a valid type (such as int, float...), name is a valid identifier and the elements field (which is always enclosed in square brackets []), specifies the length of the array in terms of the number of elements.
So I understand that the type does not depend on the number of elements. Please try to explain.
Thanks in advance!
But if the type is char[20], why can't I declare something like: char[20] exam; ?
That's not the correct syntax. The correct syntax is char exam[20]. You could also do something like this:
using char_array = char[20];
char_array exam;
So I understand that the type does not depend on the number of elements.
That's exactly the opposite of what's the case. Arrays of different sizes have completely different types. Therefore, char[20] and char[21] are completely different types.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
if(b[s][e]!=0)
{
return b[s][e];
}
else
{
int b[s][e]=palin(str,s+1,e-1)+2;
}
I am initializing this array with a value that is returned by function palin and it is giving the following error:
[Error] array must be initialized with a brace-enclosed initializer
With
int b[s][e]=...;
you define a new array of s arrays of e integer elements.
If you want to assign a value to b[s][e] just do it:
b[s][e] = ...;
You may not use:
int b[s][e]=palin(str,s+1,e-1)+2;
That defines a new array.
Perhaps you meant to use
b[s][e]=palin(str,s+1,e-1)+2;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
why does this code does not compile?
int substring(char * text, int k, int m, (char *) patterns[]) {
//stuff
// and example return is -1, meaning the sub string was not found
}
I know that the parenthesis in the (char *) is the problem, but I can't seem to figure it out why? I try declaring making patterns as a string in C++ and C but it doesn't compiles.
the error message is "error: expected declaration specifiers or '...' before '(' token" . It's algorithm for my class and my professor gave us that function with those parameters. in addition "patterns is an array of k pointers to \0-terminated strings of length m. I would just like to know why the given function does not compiles even if you just write return -1.
Type specifiers shall not be enclosed in parentheses in declarations. You declared a function. It is not a call of a function with arguments when you indeed could apply a casting.
There is no sense to reinterpret a two-dimensional character array or an array with elements of type char * containing strings as a one-dimensional array.
Take into account that this declaration of the parameter
char * patterns[]
is equivalent to
char ** patterns
char * is a type declaration, not a type cast (i.e. conversion).
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
Let's take the following code for example
int number = 1;
char * charsequence = (char *)&number //casting the address of number to char *
std::cout << charsequence << endl;
The above snippet produces the following result:
☺
If I change the number then a different symbol appears.
That's fine, the real question is why do I always get the same symbols even though the memory location (&number) is different on each run? And the most important part is why do I get symbols instead of my memory address?
I assume that the casting I did is not working as I thought it was.
Edit: Since it's closed as unclear of what I'm asking, here's there real question:
How do i print the memory address of an object to the console?
At the time of this edit, this question was already answered. See the accepted answer
std::ostream has a special overload for operator<< which, when you give it a char *, will not print the value of the pointer, but instead assume that you gave it a pointer to the first element in an array of characters that is null-terminated, and then try to print the entire array up to the terminator. (This overload allows you to print C strings in a sort-of natural looking syntax.)
Since your pointer does not in fact point to the first element of a null-terminated array, your program has undefined behaviour.
If you want to print the pointer value, you should use a void pointer:
std::cout << static_cast<void*>(&number) << "\n";
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying initialize element of array
array[m][n] == char("X");
after printing that element i'm getting the value of it equals д (Russian d); how deal with it, and I'm not even able to initialize that element without parsing const char to char.
You have to write simply as
array[m][n] = 'X';
where 'X' is a character literal.
Or if you like very much string literals then:)
array[m][n] = *"X";
or
array[m][n] = "X"[0];
EDIT: I am sorry. You have also to use the assignmnet operator (=) instead of the comparison operator (==)