It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
file 1:
int mango[100];
file 2:
extern int *mango;
...
/* some code that references mango[i] */
When both are accessed as *(mango+i) why do we get error?
Also if char mango[5]; then array starts at location mango such that mango=&mango[0]. So variable 'mango' contains address to itself or the first character of array?
If you define a variable a such as:
char a[5];
Then you have defined an array of five (5) characters. The address of the first character, which can be accessed via a[0] or simply *a, is named "a". The array is also called "a" because the array is named after the address of its first element, as given in the array declaration by the programmer.
It is not possible to change the address of a by assigning to a. If you attempt to write:
a = NULL;
The error you receive should indicate that an lvalue (value which may appear left of an assignment operator) is required.
The variable a represents an array of characters, and as-such, it is a variable that is bound to the address that holds the value for the first character of the array. When array-names are passed as arguments to functions, or used on the right-hand side of the assignment operator, they decay (i.e., are implicitly converted) into pointers to the first element of the array. Thus arrays are not pointers, but because of the implicit conversion, they can be used like pointers in many C and C++ operations.
That being said, if you are defining char a[4]; in one .c file, and then including that declaration in another .c file that has a variable called char* a, then the C-compiler is going to complain about multiple definitions of the same variable.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
If I declare a character array: char arr[200]
and then I subsequently use the function cin to read values into arr[200]
and I type into the command window line: abcd
Is there a null terminated: \0 automatically added to the array at the end of the input?
(I don't think so because I tested it using the cin function: cin>>abcd )
Can somebody explain it to me why?
Below is a snippet of my code I use to test
char arr[200]
int count=0;
int i=0;
cin>>arr // i type into command window:abcd
while (arr[i] != '\0')
{
count++;
i++
}
My count value will not be 4 but like 43 hence I concluded that the character array is not null terminated after the cin function
Formatted input from a std::istream into a character array will null-terminate the input, as specified in C++11 27.7.2.2.3/9:
operator>> then stores a null byte (charT()) in the next position
The code you've posted gives the expected result once the obvious syntax errors are fixed. But beware that this is very dangerous; there is no check on the length of the array, so too much input will overflow it. I strongly recommend you use the std::string class, rather than plain character arrays, for managing strings.
The code you posted in a comment via a link looks like this:
char array[20];
int length=getlength(array);
cin>>array;
reading into the array after attempting to measure the string length of the uninitialised array. This could give any result, or crash, or cause any other example of undefined behaviour.
In future, you should make sure that the code you post in your question is the same code that exhibits the behaviour you're asking about; otherwise, it's impossible to answer the question.
Yes, the input will be zero-terminated. Otherwise you wouldn't be able to, for example, print it without printing random characters after your input.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array of strings and an array of ints. Usually when I want to get a certain char from string I use:
string[char_position];
But when I have two arrays the same way of getting char doesn't work:
string[i][char_position[i]];
How can I get certain char from array of strings?
Try it like this instead:
string[i][char_position];
You sholdn't be subscripting char_position.
In first example you are using char_position as int but in second you are using it as array. Either one of this is wrong obviously if you mean same variable in two cases. But what it looks like you want to access jth charecter of ith string. If it is that then
string[i][j]; // j is position of char in string[i]
string[i][char_position[i]]; means the instructions:
get the INTEGER from char_position array indexed at i
get the character in ith string, in the position got from the first step
This is valid and correct ONLY if char_position is an ARRAY and not a SCALAR VARIABLE.
On the other hand, if char_position IS a scalar variable THEN:
string[i][char_position] is the way to retrieve char_positionth character in the ith string, in the array of character arrays string
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
warning: argument to `int' from `lua_Number'
I get these warnings from using the lua_tonumber function. What is the problem?
When you want an int from a lua_Number, use lua_tointeger.
From http://pgl.yoyo.org/luai/i/lua_tonumber:
lua_Number lua_tonumber (lua_State *L, int index);
It wants an int for the 2nd parameter. Your warning says, helpfully, "argument to int' fromlua_Number'". So you're probably passing in a 'lua_Number' for the 2nd parameter, rather than an int. a 'lua_Number' is a double.
Since it's an index number into Lua's stack, passing a double doesn't make any sense. I would check your code as passing in a lua_Number is probably a mistake.
Based on the provided and spare informations, my answer is this:
lua_Number is a double. So it is complaining about getting converted to an int.
The easiest method to convert it is this:
lua_Number a = 3.7;
int b;
b = static_cast<int>(a)
However, b will be 3. If you want to round it, you can do something like this:
lua_Number a = 3.7;
int b;
b = static_cast<int>(a+0.5)
This way you can ensure that every number which has a suffix higher or equal to 0.5 is rounded.
But you need to decide yourself, what kind of solution you want here.
Try static_cast? It usually suppresses warnings, but I'm not familiar with Lua.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Please explain following things in regard of C or C++:
The null pointer
The internal representation of a null pointer
The null pointer constant - 0
The NULL macro
The ASCII null character (NUL)
The null string ("").
The null pointer is a pointer that doesn't point to any object.
The internal representation is not specified, and specifically doesn't have to be all bits zero.
The value 0 can be converted to a null pointer.
The NULL macro is defined as a value that can be converted to a null pointer, in C++ it is often just 0 and in C often (void*)0, but can be other values as well if the implementation decides so.
The NUL character is a character that has the value 0 or '\0'.
The string "" is just an empty string.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
There are surely many ways how to use strings in C++: char*, w_char*, CString, std::string, then some typedefs like LPTSTR and other permutations of letters that no one except experienced C++ programmers understands.
I remember reading an article (a long time ago), where the author was criticising C++ for its inconsistency when dealing with strings. He gave an exact number of various string types in C++. I would like to either find this article, or some other reference that provides a number of string types available in C++.
Because obviously, one can create yet another typedef any time, the question is:
What is the highest lower bound of number of generally used string types known today in C++?
How many string types exist in C++?
Three string types are defined in the language (see Wikipedia):
std::string (a typedef for basic_string<char>)
std::wstring (a typedef for basic_string<wchar_t>)
basic_string (a class template)
A char * is not a string. It is a pointer to a char.
A w_char* is not a string. It is a pointer to a w_char.
CString is not defined in the language.
Of course I could create my own string-like type: CCoolString. When you append an 's' to it, it is stored as a 'z'. But it's not part of the language itself - it's just my code. Internally, it would use an std::string.
What is the highest lower bound of number of generally used string types known today in C++?
Not a real question. It cannot be answered, because it depends on the users.
There is no default string type in C++. Using char *, and w_char* one can create whatever amount of string he/she wants. std::string, CString and LPTSTR are all part of different libraries, which I am not sure are part of the standard. (They may be or they may be not).
You can create your own MarekString which can also be a string type.
And in that C++ is really inconsistent, even I remember a suggestion from Joel Spolsky to make it some default data type.
P.S. you forgot char[] and w_char[], which are slightly different than there * counterpart.