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
In C I wish to create a data type (like int or float) that has only 1byte. How can I possible do this? I've tried with malloc() but didn't work that way I tried.
Could you please give me a hand here?
Example:
sizeof(int) = 4 bytes
sizeof(char) = 1 byte
sizeof(float) = 4 bytes
sizeof(myDataType) = 1 byte
There is such data type. char is always guaranteed to be one byte long. If you want another name for that type, just use typedef and create a new type based on char.
More detailed explanation can be found in this question: Are there machines, where sizeof(char) != 1, or at least CHAR_BIT > 8?
Related
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 4 years ago.
Improve this question
Im getting 10 bytes of data in an char array like which contains hex value
Data1[0] = 0x00,Data1[1] = 0x00,Data1[0] = 0x9 Data1[2]=0x01and so on...
Now I want to get this different array bytes into single long variable . Like
Long_var = 091...
How can do it any method can be accepted.
Sorry, i forgot to mention, i want to do this in 8051 code
There are generally two ways to do type punning in C, both involving arrays.
The first is to use a plain array of 32-bit integers, and then copy the bytes into that array:
char data[12];
// data is initialized...
uint32_t integers[3];
memcpy(integers, data, 12);
printf("First value is 0x%08x\n", integers[0]);
The other way is to use unions:
union type_punning_union
{
uint32_t integers[3];
char data[12];
};
union type_punning_union u;
// Initialize u.data...
printf("First value is 0x%08x\n", u.integers[0]);
Big important note 1: Your byte array have a size mismatch for matching all data evenly to 32-bit integers.
Big important note 2: The code shown above doesn't care about endianness, meaning the results printed might not be exactly what you expect.
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 7 years ago.
Improve this question
#define SRC_ASCLIN_ASCLIN0_RX (*(volatile Ifx_SRC_SRCR*)0xF0038084u)
Here SRC_ASCLIN_ASCLIN0_RX means ASCLIN(Async/Sync serial LIN Comm) Receive Service Request.
I know that the macro is used to point at the address 0xF0038084u. But I want real time examples.
Am working on UART development on Infineon microcontroller.
The macro, when expanded by the preprocessor, cast the integer literal as an address, a pointer to Ifx_SRC_SRCR, then dereferences the pointer so you can get or set the value of the memory stored at that specific address.
So you could write e.g.
Ifx_SRC_SRCR value = SRC_ASCLIN_ASCLIN0_RX;
or
SRC_ASCLIN_ASCLIN0_RX = some_other_value;
It basically equivalent to doing e.g.
int an_integer = 6;
int *pointer_to_an_integer = &an_integer;
*pointer_to_an_integer = 10; // Equivalent to SRC_ASCLIN_ASCLIN0_RX = some_other_value above
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 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 9 years ago.
Improve this question
In the Samsung R&D placement test a question was there:
What's the maximum size allocated by calloc?
a)16k b)32k c)64k d)128k
I dont know the right answer. Can anyone tell me the right answer for it.
This is a nonsense question, as it is highly system-dependent. None of the answers is right; there are system where you can calloc() mega- and gigabytes of memory.
Theoratically the maximum is the value one could request. For
void *calloc(size_t nmemb, size_t size);
that is:
nmemb * size
SIZE_MAX * SIZE_MAX
With SIZE_MAX being the limit of size_t.
However as size_t is the result of the sizeof operator the theoratical maximum is limited to SIZE_MAX.
It's related to your compiler and your machine architecture,For example:
int = 2 byte in some machine
int = 4 bye in some machine