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 (==)
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 10 months ago.
Improve this question
I'm trying to make a login/register project and I have difficulties in declaring the char* tempUsername from this code (SIGSEVG segmentation fault)
char *tempUsername, *tempPassword, *tempPasswordConfirm, *tempSecurityQuestion;
/*
no other declaration for tempUsername here
*/
std::cout<<"Enter your new username:\n";
std::cin>>tempUsername;
//process stops here
if(fileSearch(newFilename(tempUsername))) {
std::cout<<"Username already exists! Choose another username!\n";
}
else {
std::cout<<"Enter your password:\n";
std::cin>>tempPassword;
std::cout<<"Confirm your password:\n";
I'm having a hard time understanding anything about pointers, so any advice is more than helpful!
char *tempUsername
std::cin>>tempUsername;
The problem here is that your pointer is uninitialised. When you try to extract from the input stream into the uninitialised pointer, the behaviour of the program will be undefined. Don't do this.
Your goal seems to be to read a string of user input. A solution that I can recommend is to use the std::string class:
std::string tempUsername;
std::cin >> tempUsername;
No need to use a pointer to achieve this.
I can use a char* as an array of chars, is that true?
It is not true in general. If you have a char* that points to an element of an array of char, then you can use that char* as an iterator to access the elements of the array.
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 2 years ago.
Improve this question
I am new at the c++. I made basic random string program but I cant print the string to console. These are my codes :
you did not include <string> and also you take the string by copy, to edit it you'll have to pass it by reference using &
#include <string>
void randomString(std::string& line)
you don't need to assign a number to int and then assign it to char, char is an integer value in c++ so you can:
char character = rand()%122 + 97;
with this method your random numbers will not be very good, you can look into How to generate a random number in C++?
you access your string with [] operator and it has not yet been defined, to add a character to a string just use
line += character;
also if you want a random number length, there is no need for the boolean and i++ stuff:
int charNumb = rand() % 8 + 4;
while(charNumb--)
will do just fine and it looks much cleaner.
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
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'.
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";