Confusing Nulls in c [closed] - c++

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.

Related

String Encode in C++ [closed]

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 9 years ago.
I need to convert the string "Test €" to "Test &#8364 ;" and vice versa. Please make a note that its a string and not from the xml. For your information I am developing the application in C++ and using Xerces also for XML Parsing. Please help me how it can be achieved in c++ application.
Thanks,
Ram
I think this answer may be platform-dependent, though I don't know for sure.
You can use stringstreams and casting. If lookup is a string holding the decimal version of the character code, this function will return the character version:
char fixchar(string lookup){
stringstream converter (lookup);
int i;
converter >> dec >> i;
return (char)i
(Note that for hex strings, which are prefixed with #x instead of #, you can just use hex instead of dec).
You can get the lookup strings by using the find function on the original string. Here's a loop that uses the above function to convert a string (called fixd) with &#x[number] substrings into a normal string with no character codes:
while (fixd.find("&#x")!=string::npos){
tag = int(fixd.find("&#"));
endtag = int(fixd.find(";"));
fixd = fixd.substr(0,tag) + fixchar(fixd.substr(tag+3,endtag-tag-3)) + fixd.substr(endtag+1, fixd.length()-tag-4);
}
Similarly, you should be able to get the int version of a character just by casting it, after which you can do whatever you want with it, including adding it in decimal form to a string.

Does the cin function add null terminated at the end of input? [closed]

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.

Getting char from array of strings [closed]

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

how to remove Ì from data in C [closed]

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 am getting a value like this
"RE000022000500200Ì 0.00 0.1 0.129.8#####- 97- 2#####- 1##### 960.504.00 0: 00.000.00 8: 013:52 0: 021:52############2.00.0 "
I want a value like this after doing some processing on above data can you You please what should be the logic to remove "Ì"
"RE000022000500200 0.00 0.1 0.129.8#####- 97- 2#####- 1##### 960.504.00 0: 00.000.00 8: 013:52 0: 021:52############2.00.0 "
If there's no character that's value is 0xC3 in your data, you can traverse your data string, if an character that it's value equals 0xC3 occur, then remove the character and the next character.
It's easy enough if you are using std::string to hold your value.
#include <string>
#include <algorithm>
std::string input = ...;
input.erase(std::remove(input.begin(), input.end(), 'Ì'), input.end());
It's more complex if you insist on using C strings or arrays.
I see from the comments above that you are using C strings. I suggest you switch to using C++ strings.

difference between way a[i] and *(a+1) are handled [closed]

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.