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
I know what puts and gets do, but I don't understand the meaning of this code.
int main(void) {
char s[20];
gets(s); //Helloworld
gets(s+2);//dog
sort(s+1,s+7);
puts(s+4);
}
Could you please help me to understand?
Draw it on paper, along these lines.
At first, twenty uninitialised elements:
| | | | | | | | | | | | | | | | | | | | |
gets(s):
|H|e|l|l|o|w|o|r|l|d|0| | | | | | | | | |
gets(s+2):
|H|e|d|o|g|0|o|r|l|d|0| | | | | | | | | |
^
|
s+2
sort(s+1, s+7):
|H|0|d|e|g|o|o|r|l|d|0| | | | | | | | | |
^ ^
| |
s+1 s+7
puts(s+4):
|H|0|d|e|g|o|o|r|l|d|0| | | | | | | | | |
^
|
s+4
The best thing to say about the code is that it is very bad. Luckily, it is short but it is vulnerable, unmaintainable and error prone.
However, since the previous is not really an answer, let's go through the code, assuming the standard include files were used and "using namespace std;":
char s[20];
This declares an array of 20 characters with the intent of filling it with a null-terminated string. If somehow, the string becomes larger, you're in trouble
gets(s); //Helloworld
This reads in a string from stdin. No checks can be done on the size. The comment assumes it will read in Helloworld, which should fit in s.
gets(s+2);//dog
This reads in a second string from stdin, but it will overwrite the previous string starting from the third character. So if the comment is write, s will contain the null-terminated string "Hedog".
sort(s+1,s+7);
This will sort the characters in asserting ascii value from the second up to the seventh character. With the given input, we already have a problem that the null-character is on the sixth position so it will be part of the sorted characters and thus will be second, so the null-terminated string will be "H".
puts(s+4);
Writes out the string from the fifth position on, so until the null-charater that was read in for "Helloworld", but then overwritten and half-sorted. Of course input can be anything, so expect surprises.
gets(s); //Helloworld -- reads a string from keyboard to s
gets(s+2);//dog -- reads a string from keyboard to s started with char 2
sort(s+1,s+7); -- sorts s in interval [1, 7]
puts(s+4); -- writes to console s from char 4
gets(s); //Helloworld --> s=Helloworld
gets(s+2);//dog --> s=Hedog
sort(s+1,s+7); --> s=Hdego
puts(s+4); --> console=Hdego
Related
Provided that texts is an array of 3 strings, what's the difference between &texts[3] and (&texts)[3]?
The [] subscript operator has a higher precedence than the & address-of operator.
&texts[3] is the same as &(texts[3]), meaning the 4th element of the array is accessed and then the address of that element is taken. Assuming the array is like string texts[3], that will produce a string* pointer that is pointing at the 1-past-the-end element of the array, ie similar to an end iterator in a std::array or std::vector.
----------------------------
| string | string | string |
----------------------------
^
&texts[3]
(&texts)[3], on the other hand, takes the address of the array itself, producing a string(*)[3] pointer, and then increments that pointer by 3 whole string[3] arrays. So, again assuming string texts[3], you have a string(*)[3] pointer that is WAY beyond the end boundary of the array.
---------------------------- ---------------------------- ----------------------------
| string | string | string | | string | string | string | | string | string | string |
---------------------------- ---------------------------- ----------------------------
^ ^
&texts[3] (&texts)[3]
The string below is probably the result of bad API call:
_±êµÂ’¥÷“_¡“__‘_Ó ’¥Ï“ùü’ÄÛ“_« “_Ô“Ü“ù÷ “Ïã“_÷’¥Ï “µÏ“ÄÅ“ù÷ “Á¡ê±«“ùã ê¡Û“_ã “__’
I am not sure which rows contain non-alphanumeric characters and my task is to identify which rows are problematic.
Another problem is that some non-alphanumeric characters appear with strings that I would like to still keep and search, like:
This sentence is fine and searchable, but a few non-alphanumeric äóî donäó»t popup
Is there a way to test if the entire contents of a string are non-alphanumeric?
You can use a regular expression to find all rows with only standard alphabetic and numeric characters including commas, periods, exclamation and question marks as well as spaces:
clear
input str168 var1
"_±êµÂ’¥÷“_¡“__‘_Ó ’¥Ï“ùü’ÄÛ“_« “_Ô“Ü“ù÷ “Ïã“_÷’¥Ï “µÏ“ÄÅ“ù÷ “Á¡ê±«“ùã ê¡Û“_ã “__’"
"This sentence is fine and searchable, but a few non unicode äóî donäó»t popup"
" This is a regular sentence of course"
" another sentence, but with comma"
" but what happens with question marks?"
" or perhaps an exclamation mark!"
end
generate tag = ustrregexm(var1, "^[A-Za-z0-9 ,.?!]*$")
. list tag, separator(0)
+-----+
| tag |
|-----|
1. | 0 |
2. | 0 |
3. | 1 |
4. | 1 |
5. | 1 |
6. | 1 |
+-----+
Another possibility is to use a regular expression to exclude any rows that do not have any alphabetic and numeric characters, a solution which in this case covers both required cases:
clear
input str168 var1
"_±êµÂ’¥÷“_¡“__‘_Ó ’¥Ï“ùü’ÄÛ“_« “_Ô“Ü“ù÷ “Ïã“_÷’¥Ï “µÏ“ÄÅ“ù÷ “Á¡ê±«“ùã ê¡Û“_ã “__’"
"This sentence is fine and searchable, but a few non unicode äóî donäó»t popup"
" This is a regular sentence of course"
" another sentence, but with comma"
" but what happens with question marks?"
" or perhaps an exclamantion mark!"
"¥Ï“ùü’ÄÛ“_« “_Ô“Ü“ù÷ "
"¥Ï“ùü’ÄÛ hihuo"
end
generate tag = ustrregexm(var1, "^[^A-Za-z0-9]*$")
list tag, separator(0)
+-----+
| tag |
|-----|
1. | 1 |
2. | 0 |
3. | 0 |
4. | 0 |
5. | 0 |
6. | 0 |
7. | 1 |
8. | 0 |
+-----+
This question already has answers here:
What does int argc, char *argv[] mean?
(12 answers)
Closed 6 years ago.
What I understand is argc holds total number of arguments. Suppose my program takes 1 argument apart from program name. Now what does argv hold? Two pointer eg: 123,130 or ./hello\0 and 5. If it holds 123 how does it know it has read one argument? Does it know because of \0.
If all the above is wrong, can someone help me understand using memory map.
The argv array is an array of strings (where each entry in the array is of type char*). Each of those char* arrays is, itself, NUL-terminated. The argv array, itself, does not need to end in NULL (which is why a separate argc variable is used to track the length of the argv array).
In terms of those arrays being constructed to begin with, this is dependent on the calling program. Typically, the calling program is a shell program (such as BASH), where arguments are separated via whitespace (with various quoting options available to allow arguments to include whitespace). Regardless of how the argc, argv parameters are constructed, the operating system provides routines for executing a program with this as the program inputs (e.g. on UNIX, that method is one of the various variations of exec, often paired with a call to fork).
To make this a bit more concrete, suppose you ran:
./myprog "arg"
Here is an example of how this might look in memory (using completely fake addresses):
Addresss | Value | Comment
========================
0058 | 2 | argc
0060 | 02100 | argv (value is the memory address of "argv[0]")
...
02100 | 02116 | argv[0] (value is the memory address of "argv[0][0]")
02104 | 02300 | argv[1] (value is the memory address of "argv[1][0]")
...
02116 | '.' | argv[0][0]
02117 | '/' | argv[0][1]
02118 | 'm' | argv[0][2]
02119 | 'y' | argv[0][3]
02120 | 'p' | argv[0][4]
02121 | 'r' | argv[0][5]
02122 | 'o' | argv[0][6]
02123 | 'g' | argv[0][7]
02124 | '\0' | argv[0][8]
...
02300 | 'a' | argv[1][0]
02301 | 'r' | argv[1][1]
02302 | 'g' | argv[1][2]
02303 | '\0' | argv[1][3]
I have a 2d array that represents a map/maze that looks like this :
+-+-+-+-+-+
| |
+-+ +-+ + +
| | | |
+ +-+-+ + +
| | |
+-+ +-+-+-+
And I have the following code for reading that map :
char mapa[hlimit][wlimit];
for(int j=0;j<hlimit;j++)
cin>>mapa[j];
also tried this :
char mapa[hlimit][wlimit];
for(int j=0;j<hlimit;j++)
for(int k=0;k<wlimit;k++)
cin>>mapa[j][k];
Both ways the for loop ends before I enter the whole map. I tried replacing the blank spaces in the map with dots and the input works flawlessly. So, how do I do the input with spaces? I tried cin.getline(mapa[j],wlimit) also, didn't work for me.
The answer provided by WhozCraig suggesting using get() worked flawlessly. Here's my code :
char mapa[hlimit][wlimit];
for(int j=0;j<hlimit;j++)
{
for(int k=0;k<wlimit;k++)
mapa[j][k]=cin.get();
cin.get();
}
This question already has an answer here:
"Fun" C++ library that interprets ASCII figures in code - what is it called? ("Multi-Dimensional Analog Literals")
(1 answer)
Closed 9 years ago.
If I recall correctly, I saw some ungodly C++ library that let you type ASCII-art shapes in C++ programs and treat them as objects. Something like this:
int area = someFreakyClass(o-----o
| |
o-----o).area();
What was this library called?
Analog Literals by Eelis.
Improved version of Analog Literals:
Tweaking Analog Literals (C++ humor)
Something like this:
int area = someFreakyClass(_________________________________________________
/| | |
|| | |
.----|-----,| |
|| || ==|| |
.-----'--'| ==|| |
|)- ~| ||_________________________________________________|
| ___ | |____...==..._ >\______________________________|
[_/.-.\"--"-------- //.-. .-.\\/ |/ \\ .-. .-. //
( o )`==="""""""""`( o )( o ) o `( o )( o )`
'-' '-' '-' '-' '-').area();