Sep 29, 2022 01:27
You already have code that asks for user input. So what stops you from doing the same for num?
Sep 29, 2022 01:27
Can't answer that question as you are the one writing the code and you haven't told us what the program is trying to do and what num is supposed to be. It needs to be set to whatever value your program requires it to be. Or read from user input. Or whatever the required logic is to solve the problem you are addressing.
Sep 29, 2022 01:27
No, that declares a variable but does not set its value. You need to assign a value with = or scanf into that variable or some other way to actually set the value.
Sep 29, 2022 01:27
Perhaps you should pause programming and go thru a basic C book/tutorial. Do you understand what "initialization" means? It means assigning a value to the variable. Where in your code do you set num?
Sep 29, 2022 01:27
Suggest you learn to read the build log for errors. Seems pretty clear: uninitialized local variable 'num' used
Sep 29, 2022 01:27
Bottom left hand corner window in your screen shot. That's where it shows any build errors.
Sep 29, 2022 01:27
Please provide the full Output window log. As text.
Sep 29, 2022 01:27
And what about the obvious error in the code pointed out in the previous comment? You have not provided any response to that.
Sep 29, 2022 01:27
Look at the errors in the output window. It seems the code has compilation errors. There is a typo pritnf instead of printf. For further help, please provide the code and error messages as text and not screen shots.
 
Mar 30, 2022 19:40
Do you know what T10 refers to?
Mar 30, 2022 19:40
Where is that error message coming from?
Mar 30, 2022 19:40
Please show the exact and complete code as a minimal reproducible example. Also show the exact error message. The message refers to _content which implies you have mistyped something (_ should not be there) and/or it is referring to code you have not shown (e.g what is T10?)
 
Mar 7, 2022 03:29
By accident/luck. You are doing random things with no science behind it. You are making incorrect conclusions based on one single example. The compiler is free to arrange the memory/stack/registers differently with different code.
Mar 7, 2022 03:29
"first line gets the address pointed to by g in main". No it doesn't do anything of the sort. You are making assumptions that you have no right to.
 
Jan 26, 2022 16:41
Please address the need to copy into a single line. Why is that needed? I'm afraid that you may be wasting effort and asking the wrong question. Seems like a possible XY problem
Jan 26, 2022 16:41
Firstly, I'm not convinced you really need to copy into one single line. Why can't you tokenise one line at a time? Anyway, it's not clear what is difficult about strcat. You just make an array initialised to empty string: char line[MAX_LINE_LEN] =""; and then in the loop do strncat(line, buffer, MAX_LINE_LEN)
Jan 26, 2022 16:41
 
Oct 26, 2021 23:32
I never said it is wrong. Just not sure what your code is meant to do (it doesn't seem to make sense to just run the inner loop forever). But yes you can set to 0 there too.
Oct 26, 2021 23:32
unsigned char RESULT = 0;
Oct 26, 2021 23:32
Please heed the repeated requests to post the code as text into the question.
Oct 26, 2021 23:32
Please do not post code as an image - reasoning. Copy it as formatted text into the question.
Oct 26, 2021 23:32
The RESULT variable is uninitialised. So code that reads it is using indeterminate value. Set it to 0 at the start. There is also an extra ; at the end of the for line which means the next line is not actually in the loop body.
 
Aug 30, 2021 09:22
Nowhere in that description does it say you can't use other standard functions.
Aug 30, 2021 09:22
How about reading the stdout from the child process? Such as via popen? People may be able to give better suggestions if you provide more context.
 
Jan 26, 2021 00:47
I gave you a link that explains what to do. Please read it. something like: int readLines(struct line **ln, FILE *f) { *ln = realloc() } and call with readLines(&ln, f);
Jan 26, 2021 00:47
The memory is still allocated. But the calling function has no reference to it and cannot access it (so that's a memory leak as the memory is lost).
Jan 26, 2021 00:47
when I printed the data from the ln array, it worked. Printed it where? Inside the function? Do you understand why it is wrong? Inside the function is ok but not outside the function. You can't set a function parameter and expect the caller to see the result. See: Changing address contained by pointer using function.
Jan 26, 2021 00:47
Did you see my comment regarding readLines being wrong? For further help please post complete code. It is difficult to debug incomplete code snippets. Please see How to create a Minimal, Reproducible Example.
Jan 26, 2021 00:47
sizeof(ln) should be sizeof(*ln). The former gives the size of the pointer whereas the latter gives the size of the struct. The latter is what qsort needs.
Jan 26, 2021 00:47
This is wrong: readLines(ln, f);. In C, all function parameters are passed by value. So there is no way that the function can change the value of ln to return the allocated memory.
 
Oct 23, 2020 01:04
Why are you asking all these things? You have an answer below that shows a full solution that should answer that already. Learn from that. And really, pause coding and (re)do a basic tutorial to improve your C language fundamentals first.
Oct 23, 2020 01:04
If you are saying that you did return outputnum and that main is using that to print then of course it is the last number. Because the loop will keep overwriting outputnum and it will be the last number when the loop ends. Or are you saying it's printing the same last number 20 times in the loop (I don't believe that would be the case)?
Oct 23, 2020 01:04
Did you check the fopen and fscanf return values? It sounds like the file read failed so it's just printing garbage. Please do basic error checking before proceeding any further. You can't just code by guessing what is going wrong.
Oct 23, 2020 01:04
Maybe your fopen failed. Always check function return values. In this case check return values of fopen and fscanf.
Oct 23, 2020 01:04
Yes you do to clean things up. But that should not be a factor for the current issue as the close should be done after reading and printing all the values. And whether the file is closed or not at the end would not change the read/print results.
Oct 23, 2020 01:04
One step at a time. Have you removed the return and just replaced it with printf("%d\n", outputnum); to see the numbers being read? If that works you know you are reading all the numbers correctly. After that you can decide what to do with each number as you read it.
Oct 23, 2020 01:04
No offense, but it sounds like you haven't actually spent time learning the language basics. Just trying to guess isn't a good way to do things. Suggest you pause programming and go through a simple C book or tutorial first.
Oct 23, 2020 01:04
What exact result do you want? That is, what is your function supposed to return or print?
 
Aug 17, 2020 14:39
We can't be expected to show you everything from scratch. Please attempt one step at a time, show your attempt and then ask a specific question about the step you get stuck on. For example, write some code to read in one line of input and print it out. Then extend to break up the line and print that out. Then extend to storing that into a single dimensional array, etc. That is, give us something to work with rather than just a general "help me".
 
May 8, 2020 00:38
I think you are missing my point. I totally agree it needs to be fixed. What I'm not sure of is whether doing that would be sufficient to fix the OP's problem. That is, do you see any other potential issue that could result in the problem described or is it certainly the -1 case that is the root cause? Anyway, hopefully fixing that will indeed resolve the problem.
May 8, 2020 00:38
Hmmm, that last comment doesn't make sense to me. Even the waitpid man has example code that does exactly that WIFEXITED check when waitpid returns 0.
May 8, 2020 00:38
I understand that and agree that could be happening. But I can't see from the code why waitpid would be returning -1 in the OP's code. It may be, but just not obvious to me.
May 8, 2020 00:38
I agree with the return value problem and was going to comment on that too. But what isn't clear to me is how that would result in TERMINATED being set because presumably the -1 case would not come into play if the process is still running?
 
Apr 10, 2020 14:01
mergeSort is typically implemented in place. That means no new memory and no copying of the original array into a new array.
Apr 10, 2020 14:01
"segfaulted pretty fast because I am not allowed to use VLA". Why do you think it is related to not using VLA? If you really think that is the problem then use dynamic allocations (ie. malloc).
 
Mar 15, 2020 03:27
If s is a valid pointer and you increment it you can't get NULL. It's basic pointer arithmetic. char *s="ab". At that point s will have the value of the memory address which stores "ab". That is *s is a. When I increment s it will point to b. Finally if I increment it again it will point to \0 (the NUL). s itself is not NULL. *s is the one that is the zero value.
Mar 15, 2020 03:27
@AdamZahran No it's not the right. For a string char *s you need to dereference s to check each character for NUL. That is, the check is for *(s+i) and not checking s itself.
Mar 15, 2020 03:27
Consider. If I start with a value of p which is a valid pointer say 1. If I increment the pointer I get 2 and so on. You guys are thinking of an array of pointers. Which we don't have.
Mar 15, 2020 03:27
No it's not correct. This is why pointers shouldn't be typedefed as it really confuses people. The function takes a PPOST. Which is a pointer to a struct. There is no way that value can ever become NULL again just by incrementing it (excluding pointer wrapping).
Mar 15, 2020 03:27
No its not right. He is making an array of PPOST and assigning it to a PPOST.