Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 days ago.
The community is reviewing whether to reopen this question as of 10 days ago.
Improve this question
I know strings end with '/0' in C. While I'm dynamically allocating memory for a string, how do I handle that?
Also, how would you print out a dynamically allocated string? Because I've tried the regular way to print out a string and it did not work.
For the first question, I tried:
int len;
scanf("%d", &len);
char* str = (char*)malloc(sizeof(char)*len);
for (int i = 0; i<len; ++i){
scanf("%c", (str+i));
}
For the second question, I tried
printf("%s", *str);
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 9 months ago.
Improve this question
I have some materials showing this code. what does code means by making these assignments?
char inputfilename[128];
inputfilename[0] = 0;
char *argv[128];
*argv[1] = 0;
In C, character arrays are terminated by a null character (value 0). In both cases in your example, the code initializes the strings to "empty" (with a terminator in the first element). This would prove useful in any subsequent string operations (strcat, strcpy, etc.).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
i wanna declare this array in c:
{
{{'7','P','Q','R','S'},{'8','T','U','V'},{'9','W','X','Y','Z'},{'÷'}},
{{'4','G','H','I'},{'5','J','K','L'},{'6','M','N','O'},{'×'}},
{{'1','.','?',',','!'},{'2','A','B','C'},{'3','D','E','F'},{'-'}},
{{'*'},{'0',' '},{'='},{'+'}}
}
how can I do this?
Like this
const wchar_t* array[4][4] = {
{L"7PQRS", L"8TUV", L"9WXYZ", L"÷"},
{L"4GHI", L"5JKL", L"6MNO", L"×"},
{L"1.?,!", L"2ABC", L"3DEF", L"-"},
{L"*", L"0" ", L"=", L"+"}
};
I'm using wide characters because of the funky characters you have but YMMV.
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 9 years ago.
Improve this question
an interview question on glassdoor is as follows. With my knowledge, it is hard to deduce anything out of it. What could be an appropriate question?
A macro that computes a size_t number. Putting in a loop, it casts -1
to a size_t number, making the loop impossible to start.
as suggested by Michael Aaron Safyan, following might be the case
operates in the reverse:
for (size_t i = 0; i > ((size_t) -1); i--) {}
For explanation see the answer
The issue is that size_t is unsigned, so casting -1 to it will produce the maximum-valued size_t. One would fix this case by using a signed type (such as int or ssize_t).
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 9 years ago.
Improve this question
char string[]="DGS021J0W0S1000.0S20000S3000.0S4000.0S50.00S60.00F";
how to get S[1-5]
thanks!
Use strncpy() standard function
char S[6] = {0};
strncpy(S, string+1 , 5);
If you want to copy from the beginning of the string to the 5th charachter, then your question should be
how to get S[0-4]
and not S[1-5] because array index in C start from 0 and not from 1. and the solution for this case will be
char S[6] = {0};
strncpy(S, string , 5);
I think you are looking for substring methods.
You can do it in two for loops in C.