|
You are putting holes in your arrays of even and odd elements. In order to avoid that, you need to maintain two separate variables to keep track of how many even and odd items have been added so far. Try
#include <stdio.h>
#define SIZE 20
int main()
{
int items, odd_items, even_items;
int array[SIZE], odd[SIZE], even[SIZE];
printf("enter how many array elements\n");
scanf("%d",&items);
odd_items = even_items = 0;
for (int n=0; n<items; ++n)
{
printf("enter item %d:\n", n);
scanf("%d", &array[n]);
if ( (array[n] % 2) == 0)
{
even[even_items] = array[n];
++even_items;
}
else
{
odd[odd_items] = array[n];
++odd_items;
}
}
printf("array items\n");
for (int n=0; n<items; ++n)
printf("%d ", array[n]);
printf("\n");
printf("even array items\n");
for (int n=0; n<even_items; ++n)
printf("%d ", even[n]);
printf("\n");
printf("odd array items\n");
for (int n=0; n<odd_items; ++n)
printf("%d ", odd[n]);
printf("\n");
return 0;
}
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
5, but you should get 10 for looking at code formatted this way.
|
|
|
|
|
Thank you.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Hi,
#include<stdio.h>
int main()
{
char str[20] = "Hello";
char *const p=str;
*p='M';
printf("%s\n", str);
return 0;
}
output:
Mello
As per my knowledge when we use const then we can't change a char in t then how output is Mello instead of Hello.
|
|
|
|
|
You need to read the declaration right to left:
char *const p=str;
| | | |
| | | +- the variable p
| | +----- is a constant
| +---------pointer
+------------to a character
So const here applies to the pointer p and not to the data that it is pointing to. That would be:
const char* p=str;
|
|
|
|
|
Hi Richard,
Thank you for your reply. I understood the concept clearly.
Best Regards
Niharika
|
|
|
|
|
const applies to what's on its immediate left: a pointer (* ) or a type (char and many others, even ones that you've defined). If there's nothing on its left--that is, when it appears first--const applies to what's on its immediate right.
modified 15-Feb-21 7:23am.
|
|
|
|
|
Hi Greg,
Thank you for your immediate response. I understood the point clearly.
Regarda,
Niharika
|
|
|
|
|
Hi All,
I am learning callbacks in c.I found an example in callback but could not understand .Can some one please explain me how the flow of execution happens and brief explaination of program.
PFA code what I found.
<pre>* callback.c */
#include<stdio.h>
#include"reg_callback.h"
/* callback function definition goes here */
void my_callback(void)
{
printf("inside my_callback\n");
}
int main(void)
{
/* initialize function pointer to
my_callback */
callback ptr_my_callback=my_callback;
printf("This is a program demonstrating function callback\n");
/* register our callback function */
register_callback(ptr_my_callback);
printf("back inside main program\n");
return 0;
}
/* reg_callback.h */
typedef void (*callback)(void);
void register_callback(callback ptr_reg_callback);
/* reg_callback.c */
#include<stdio.h>
#include"reg_callback.h"
/* registration goes here */
void register_callback(callback ptr_reg_callback)
{
printf("inside register_callback\n");
/* calling our callback function my_callback */
(*ptr_reg_callback)();
}
|
|
|
|
|
void my_callback(void)...
int main(void)
{
callback ptr_my_callback = my_callback;
register_callback(ptr_my_callback);
}
typedef void (*callback)(void);
void register_callback(callback ptr_reg_callback)
{
(*ptr_reg_callback)();
}
The printf s are just there so that you can observe the flow of execution.
modified 10-Feb-21 11:52am.
|
|
|
|
|
You might already be familiar with callbacks and not realize it. If you've ever used qsort() or bsearch() , or another library function that takes a function pointer as an argument to help it do its work, you have already used callbacks!
For example the signature for qsort is
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); and might be used like this
struct foo {
char key[10];
int id;
};
int cmpfoo_bykey(const void *vp1, const void *vp2)
{
const struct foo *foo1 = vp1;
const struct foo *foo2 = vp2;
return strcmp(foo1->key, foo2->key);
}
int cmpfoo_byid(const void *vp1, const void *vp2)
{
const struct foo *foo1 = vp1;
const struct foo *foo2 = vp2;
return foo1->id - foo2->id;
}
int main()
{
struct foo my_data[100];
qsort(my_data, 100, sizeof(struct foo), cmpfoo_bykey);
qsort(my_data, 100, sizeof(struct foo), cmpfoo_byid);
return 0;
}
In the above example qsort() knows how to sort an array of items in a generic way, but not how to compare items. It gets around this limitation by using a callback function, that the programmer provides, that does know how to compare items in the array. So, whenever qsort() needs to know how two items in the array compare to each other, it calls back to the code provided by the programmer to determine how the two items compare to each other.
Keep Calm and Carry On
|
|
|
|
|
I'm programming in C in an embedded environment and I have a bit-bang software UART I would like to be able to hook up to a myPrintf function, that supports the standard printf syntax, e.g.
myPrintf("%s %3.2f\n", "Pi:", 3.14159); I would like to achieve this without having to use a big (=worst case length of "sentence" to transmit in a single myPrintf call) temporary buffer (otherwise, I could just go via sprintf). What I want to achieve is that my characters are sent over my UART "on the fly", so in the example above "Pi: " would be sent out over my UART before the float number has been converted to ASCII characters. Does anybody know how I can achieve this in the easiest way possible? Is the source code for printf available somewhere for public use so I can modify it to fit my needs?
|
|
|
|
|
Quote: Does anybody know how I can achieve this in the easiest way possible?
Not sure if easiest but one common way of doing it is to have printf call a putc -type function to send out each succesive character. In your case, the "putc" function would be the UART output function.
Quote: Is the source code for printf available somewhere for public use so I can modify it to fit my needs?
Plenty of those. One I've used before in an embedded environment is for Rabbit microcontrollers. You can find the relevant code here: DCRabbit_10/STDIO.LIB at master · digidotcom/DCRabbit_10 · GitHub[^]
Mircea
|
|
|
|
|
Mircea Neacsu wrote: Not sure if easiest but one common way of doing it is to have printf call a putc -type function to send out each succesive character. In your case, the "putc" function would be the UART output function. That sounds very convenient. I am working with STM32F-microcontrollers, would you happen to know if it's easy to replace the putc-function for their printf-function?Mircea Neacsu wrote: Plenty of those. One I've used before in an embedded environment is for Rabbit microcontrollers. You can find the relevant code here: DCRabbit_10/STDIO.LIB at master · digidotcom/DCRabbit_10 · GitHub[^] Awesome!
|
|
|
|
|
Quote: I am working with STM32F-microcontrollers, would you happen to know if it's easy to replace the putc-function for their printf-function?
No idea! you said you have the bit banging UART code; you just have to use it to send out one character.
Keep in mind also what @CPallini suggested: if you don't need a very general printf function you can have with a much simpler implementation. Look at this one for an example: sdcc/printf_small.c at master · darconeous/sdcc · GitHub[^]
Also, along the same lines, you scan the format specifier string and every time you encounter a '%' character you can use sprintf to print it in a small buffer.
Mircea
|
|
|
|
|
Probably existing printf code is too general for your system, so, it is probably better to make yourself an over-simplified version of it, tailored on your needs. Anyway, you know, in order to use just a small buffer you need to append characters to the buffer while there is still available space and wait patiently the transmission of characters in order to use the buffer again. This means you can't actually use a function call similar to the printf one.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
CPallini wrote: in order to use just a small buffer you need to append characters to the buffer while there is still available space and wait patiently the transmission of characters in order to use the buffer again. This means you can't actually use a function call similar to the printf one. It's a completely synchronuous bit-bang UART where I'm counting clock cycles. There is no DMA, timer interrupts, 1 byte FIFO, threading or anything fancy.
|
|
|
|
|
So you have to it in place. Forgot the printf signature and write a function to output a string, a character at time. Then write a function to output properly a floating point number (that would be more difficult, but it depends on your requirements) and combine the two function in order to obtain the expected result.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
I made an app that used to works just fine to change gamma, and it still does, but with one condition, to have only one monitor connected. But the problem now is that I have 2 monitors, and the app doesn't work anymore, only if I disconnect one of them.
Can anyone tell me what I need to change in order to make it work with multiple display setups? And if there is a way to change gamma on only one of them then that is a good bonus and will help with another idea that I have.
This is what I'm using right now:
WORD ramp[256 * 3];
for (int i = 0; i < 256; i++) {
ramp[i] = ramp[i + 256] = ramp[i + 512] =
(WORD)min(65535, max(0, pow((i + 1) / 256.0, ValueForLight) * 65535 + 0.5));
}
SetDeviceGammaRamp(::GetDC(NULL), ramp);
|
|
|
|
|
|
I don't get it. I don't see anything there that may interfere with the app and the way I have my PC.
|
|
|
|
|
The first paragraph clearly states that you should not use it, and it may even fail without giving any indication, and indeed returning TRUE suggesting that it did not fail.
|
|
|
|
|
The following statement is a personal opinion and is not addressed to you specifically! So take it as it is!
These types of answers are actually more harmful then they are helpful. Not only that they don't actually help the person reach a solution but they may affect some, and actually will affect certain people by at least demoralizing them. We are not in school to say "I can't give you a straight answer because you are on a test" or by giving them a riddle which may lead that person on a wrong path that is even farther from the solution, we are here to learn from each other. So it is best to actually not to say anything then giving an answer that isn't helpful or by making fun of them for not knowing. Not everyone is good at searching on google for the answer, maybe they don't know exactly what to search for. Why is it so hard to tell someone, "Here you do it this way, use that and like that"?! This as a side note, because I saw in general, on other platforms as well, many answers that were so bad and actually harmful. I have a friend that tried to get into programing but quit it because he posted like only 2 questions on another popular website, and because the questions where really beginner level many started criticizing him for what he asked, like "How can you not know that?!", well he was just starting so how could he have know.
As for what you said that, in that paragraph it says not to use it, yeah it is true, but it also says the following "Use of this API is subject to major limitations", so it's not like not to use it because it is bad, but because other reasons.
As for the other part, it is actually a good thing in that situation that it doesn't uses what you give it, and it actually is telling you why in documentation, because it may lead to an unusable screen.
|
|
|
|
|
Richard posts a link to the documentation where it clearly says "We strongly recommend that you don't use this API" and then you complain that it isn't the kind of answer you wanted ?????
The documentation even goes so far as to tell you how to do what you want using a supported alternative method!!
DID YOU EVEN READ IT!?!?
|
|
|
|
|
First of, I wasn't talking about him giving a link to the documentation. That is a good replay when you don't know a solution, but you still want to try to help. I did seen it before posting my original questions, but it doesn't say anything about the problem I encountered. I even mentioned that it was a general opinion, and at the end of the statement I added something as a respond to what he said.
So back to the matter, yes they do say not to use it, but they also give you reasons why not to, which none of them are bad for the use case the app I made is meant for. So by taking in consideration their warnings in the documentation, it was still a good option to use.
Quote: The documentation even goes so far as to tell you how to do what you want using a supported alternative method!!
Yes, it does talk about some alternatives, but I don't need color calibration, or blue light filtering, or color adaptation.
Quote: DID YOU EVEN READ IT!?!?
So based on what I just said, I can ask you the same, did you read everything I said?! But as I said in that statement, this type of behavior isn't helpful so why not try to avoid it, and actually try other means of expressing to get your point of view understood, as this is something optional to do, no one is forcing you to answer every question the users of this website are posting?!
I even saw this article in which they talk about evolution of methods to do that, but there are a few problems with other methods they present, first and most important this:
Quote: The Direct3D 9 and DXGI APIs are “write only”. You can't read the value of the hardware, modify it, and then set it. You can only set the ramp.
The problem with this is that my app is meant for general gamma change, which means when the user closes the app, he has an option to restore to the original gamma value, which with those 2 you can't.
And also this problem:
Quote: Furthermore, you can only set gamma when the app is full screen.
I need it to work in general not based on an app that is running in fullscreen mode.
|
|
|
|
|