'Is '\0' in the middle of a string can be recognized as the end of a string in C?
I know in C '\0' is always at the end of a string, but is '\0' always mark the end of a string?
Just like "1230123" can be recognized as "123"?
One edition of the question used the notation '/0' instead of '\0'.
Solution 1:[1]
A byte with the value 0 by definition marks the end of a string. So if you had something like this:
char s[] = { 'a', 'b', 'c', '\0', 'd', 'e', 'f', '\0' };
printf("%s\n");
It would print abc.
This is different from "1230123" where the 4th character in the string is not the value 0 but the character '0', which has an ASCII code of 48.
Solution 2:[2]
The null terminating character is represented as \0 and not /0 and it always mark end of string because, in C, strings are actually one-dimensional array of characters terminated by a null character \0.
This
char s[] = "1230123";
is same as this
char s[] = {'1', '2', '3', '0', '1', '2', '3', '\0'};
| |
This is character '0' |
whose decimal value is 48 |
|
This is null terminating character
whose decimal value is 0
Check this example:
#include <stdio.h>
int main (void)
{
int x,y,z;
char s1[] = "1230123";
char s2[] = {'1','2','3','\0','4','5','6','\0'};
printf ("%s\n", s1);
printf ("%s\n", s2);
return 0;
}
Output:
1230123
123 <======= The characters after null terminating character is not printed.
Solution 3:[3]
A string literal can have a '\0' in the middle.
A string only has a '\0' at the end.
'\0' is the null character and has a value of 0. '0' is the character zero.
See value of '\0' is same ... 0?
C has, as part of the language, string literals.
The two string literals below have a size of 8: the 7 you see plus the 1 not explicitly coded trailing null character '\0'.
"abc-xyz" // size 8
"abc\0xyz" // size 8
C, as part of the standard library, defines a string.
A string is a contiguous sequence of characters terminated by and including the first null character.
Many str...() functions only work with the data up to the first null character.
strlen("abc-xyz") --> 7
strlen("abc\0xyz") --> 3
Solution 4:[4]
The work of PeriodId attribute and its descendants looks kind of tricky, as they share their cache. Without doing this customization and debugging, the rule of a thumb is to mimic the declaration of multiple out-of-the box DACs using these periods. I see two differencies of this declaration from standard which might play a role:
FinPeriodId generally precedes the TranPeriodId declaration. It looks important because the source field used for the periodid defaulting comes from the specification for the attribute on the FinPeriodId field
In the sourceType declaration, the DAC name is generally specified. So consider using the following syntax:
[FinPeriodID(
sourceType: typeof(PMCashflowProjectionSchedule.date),
Solution 5:[5]
You could use PXDefault attribute to set a default value on page load and if you would like to allow the users to change the default value the field can be made as a Selector type with PXSelector attribute.
an example below for PXDefault attribute getting default value from the financial periods and current business date :
[PXUIField(DisplayName = "Fin. Period", Required = true)]
[FinPeriodIDFormatting()]
[PXDefault(typeof(SearchFor<MasterFinPeriod.finPeriodID>
.Where<MasterFinPeriod.endDate.IsGreaterEqual<AccessInfo.businessDate.FromCurrent>
.And<MasterFinPeriod.startDate.IsLessEqual<AccessInfo.businessDate.FromCurrent>>>),
PersistingCheck = PXPersistingCheck.Nothing)]
[PXSelector(typeof(SearchFor<MasterFinPeriod.finPeriodID>.In<SelectFrom<MasterFinPeriod>
.Where<MasterFinPeriod.endDate.IsLessEqual<AccessInfo.businessDate.FromCurrent>
.And<MasterFinPeriod.finPeriodID.IsNotNull>>>
.OrderBy<MasterFinPeriod.finPeriodID.Desc>>))]
FinPeriodIDFormatting() attribute formats to MM-YYYY
<px:PXSelector CommitChanges="True" runat="server" ID="edTranPeriodID" DataField="TranPeriodID" ></px:PXSelector>
<px:PXSelector CommitChanges="True" runat="server" ID="edFinPeriodID" DataField="FinPeriodID" ></px:PXSelector>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dbush |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | Mark Volshteyn |
| Solution 5 | Natesan Sivarama |
