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
is there any way how to put integer sized 100 000 into 4 elements of char array? If I use sprintf or itoa, array has 6 elements. I tried to use this, but it didnt work. And is there any way how to put these 4 elements back to integer?
char *s;
int value = 100000;
*((int *)s)=value;
Note that:
int value = 100000;
char *s;
*((int *)s)=value;
dereferences uninitialized pointer s, which causes undefined behavior. You could do:
int value = 100000;
char s[4];
*((int *)&s[0])=value;
just note that this stores value in the memory block "occupied" by charr array (at memory level) unlike sprintf, which would print the value in a form of string (characters representing the number).
Related
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 am trying to create a function that will add the total of 3 exams, divide them by 4 and then multiply them by .4 and pass the resulting number back to a pointer. I'm not sure which line number is the issue or what I am missing here.
I'm new to coding in C and new to Stack Overflow.
void calcExams(void)
{
int i;
float examTotal;
float *calcExams;
float oneExam;
for (i = 0; i > 3;)
{
printf("\n Enter an exam grade: ");
scanf("%f", oneExam);
examTotal = examTotal + oneExam;
*calcExams = examTotal / 4 * .4;
}
}
Problem 1:
A pointer, as the name hints at, should point somewhere, i.e to some valid memory location, as you have it, it's uninitialized, it points nowhere, or more accurately, to some random memory location given by whatever residual value is stored in it when it's declared, accessing such memory location by dereferencing the pointer amounts to undefined behavior.
That is to say your pointer must be initialized before it's used, either by way of memory allocation or otherwise making it point to some valid variable:
#include <stdlib.h>
//...
float *calcExams = malloc(sizeof *calcExams);
Or:
float some_variable;
float *calcExams = &some_variable;
Problem 2:
scanf expects as an argument the address of the variable on which to store the inputed value, but you are passing this variable by value, you need:
scanf("%f", &oneExam);
^
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
Let's say i have a pointer-pointer-char array that looks like this:
2-abc
5.5-aaa
10-acdc
3-(the text here doesn't matter)
I need to sort the array in ascending order, acording to the number in each string. I know that the number ends with "-". The numbers can also have decimal points and are in the range of <0;INT_MAX>. Any ideas?
Use qsort with a comparison function that uses strtod to convert the initial portion of the string to a double value. Be careful to return an integer <0, ==0 or >0 depending of whether the converted values are a<b, a==b or a>b.
You need to put some work into this assignment, but it should fit in a single page of code.
Assuming the array is an array of pointers to strings, here is a comparison function you can use:
#include <stdlib.h>
int mycmp(const void *a, const void *b) {
double aa = strtod(*(const char **)a, NULL);
double bb = strtod(*(const char **)b, NULL);
return (bb < aa) - (aa < bb);
}
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 8 years ago.
Improve this question
I am finding difficulties to solve an expression please help me to do so.
Declare four variables of type char. Initialize one variable to ‘z’. Initialize the other variables to the ASCII integer value for ‘z’, the ASCII octal value for ‘z’, and
the ASCII hexadecimal value for ‘z’.
Just declare a char variable and assign the value. To assign hex prefix with 0x, for octal use 0 and for decimal just write the number with no prefix.
The decimal ascii value of 'z' is 122.
#include <stdio.h>
int main() {
char a = 122;
char b = 0x7a;
char c = 0172;
char d = 'z';
putchar(a);
putchar(b);
putchar(c);
putchar(d);
}
All these char variables have the same value so four zs will be printed.
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 8 years ago.
Improve this question
I've found this function on the internet and I find it to be very useful
but I am new to programming, and could someone please explain briefly what does it exactly do
#include <stdio.h>
int diffcount(char* s)
{
unsigned char seen[127];
int cnt=0,i;
for(i=0;i<127;i++)
seen[i]=0;
for(i=0;s[i];i++)
{
if(!seen[(int)s[i]])
{
cnt++;
seen[(int)s[i]]=1;
}
}return cnt;
}
int main(void) {
char string[20];
scanf("%s",string);
printf("Razlicitih znakova: %d\n", diffcount(string));
return 0;
}
First of all we init an empty array of zeros int seen[127];
"seen" array is used to find out whether char with code i has been met in the array s : if seen[i]==1 than (char)i was in the string s.
After that we make a loop through char* s and check if char s[i] has already been met by looking at the value of seen[s[i]]; and if it is false we put seen[s[i]]=true (because we met it) and increase our counter.
The result of the function is the value of variable cnt
This may also help:
each char has it's code between zero and 127. For example, (int)'a' = 97.
bool in the C is just the same as int, that's why we sometimes use 0 and 1 instead of true and false