John Matthews

Ranch Hand
+ Follow
since Jul 04, 2018
Was writing 6502 (8 bit) assembler fixed point arithmetic code (to draw circles) back in the early 80s, on an Acorn Atom if that means anything to anyone. Usually write embedded C, with the odd excursion into Java and C++.
Melksham, UK
Cows and Likes
Cows
Total received
4
In last 30 days
1
Total given
0
Likes
Total received
23
Received in last 30 days
4
Total given
13
Given in last 30 days
2
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Matthews

...and also, when you are partially copying s1 over to s2 at the end, you need a separate index for s2, which you only increment when you add a character to s2.

At the moment, if s1 = "cccX" where 'c' is the character to be deleted then you'll end up with s2 = "???X" where '?' is whatever happened to be in the allocated memory (which malloc() doesn't initialize by the way - doesn't matter if you get the code right). Except of course you (should) have only allocated enough memory for "X", so you could end up with anything.

(And remember that "X" = {'X', '\0'} which is why you need to have malloced 2 bytes, not 1.)

[Another question for Campbell - why does my string of 5 consecutive question marks get displayed as 3? I'm guessing a double question mark is converted to a single one, but I can't think why.]
2 days ago
Hi again Mark - first thing I can see is that it looks like you are allocating memory for N characters where N is the length of the supplied string minus the number of occurrences of the character to be deleted. Sounds ok, but you need to allocate memory for N+1 characters - strings by definition end with the '\0' character, the one you test for in the while() loop, and that *isn't* included in the value returned by strlen().

Also I don't think you are adding a '\0' to the end of your new string? You might get away with the first problem (although it still needs correcting of course) but the missing '\0' is what will really kill it.
2 days ago
Hi Tan - just wondering why you're using assembler; is it for some sort of (assembler) programming assignment so you don't have a choice? If you do have a choice, what are your options?
4 days ago

Campbell Ritchie wrote:That is the old way to use array pointers

Not sure about that - yes the original K&R book does point out that *(a+i) and a[i] are equivalent, but I can't spot, and don't remember reading, where it recommends the *(a+i) form.

Minor point. Campbell's code is much nicer, which makes it easier to read and hence spot bugs. I'm a big fan of neat code myself, as colleagues who have been at the receiving end of my code reviews will testify

[Campbell - the 'book' word above is supposed to be a URL to http://www2.cs.uregina.ca/~hilder/cs833/Other%20Reference%20Materials/The%20C%20Programming%20Language.pdf ; what have I done wrong?]
4 days ago
And I should have mentioned:
I think what you're trying to do is initially make all the pointers point to the first element in the array, hence why the for() loop starts from 1 rather than 0 - you've effectively already set the first element as the max value(s). The intention is good.

But again you're not setting where they're pointing - you're setting the contents of what they're pointing at. And in fact you haven't set them pointing at anything yet, so I wouldn't be surprised if your program crashes? That's usually what happens when you try to store something via an uninitialized pointer.
4 days ago
Hi Mark - after a quick initial look I think the problem is

The max* variables are pointers, and they point to elements in the array which hold the largest values. They are references. So when you find new largest values, you need to change where they point to, not the contents of what they are pointing at (array element values), which is what you are doing. Basically in those lines you need to drop the *s ie.
That changes where the pointers are pointing, instead of the values of what they are pointing at.

Right - hope that helps get you started, back to my own work

Will check back later.
4 days ago
Looks like a bug in that code, in removeNode() - previous needs to be initialized to NULL. Otherwise it might be uninitialized when tested at line 73 (if the key value is in the first node will be a problem I think).

Nice (very) informal description of linked lists here, don't know if it's any use:
https://www.youtube.com/watch?v=_jQhALI4ujg

There are others as well. Something more C-specific here:
https://www.learn-c.org/en/Linked_lists

Google is your friend
6 days ago
Also...

I strongly suggest adding comments to your code. It helps us understand what you're doing, and having to think about it to explain it will help you spot possible problems ( https://en.wikipedia.org/wiki/Rubber_duck_debugging ).
3 weeks ago
As it's C++, can't you use std::string for strings? That would make returning a string from a function easier/possible.

In C the traditional way to do it would be to either (1) use a static array within the function to hold the return string, which doesn't disappear when the function returns, or (2) the calling function passes in the array which the function writes the return string into. (2) is generally better, albeit slightly more complicated, because it means the function can be called from multiple threads simultaneously, and/or multiple times within the same statement of one thread. Using a static array prevents that.
3 weeks ago
Hi Padala

I got compiler errors using g++ in linux, but was able to fix them.

The first problem I can spot is that your function is returning res, which is s, which is an array in the function. That means it is on the 'stack' - not sure if you know what that means (apologies if you do) but basically it disappears when the function returns, so you can't use it to hold the return string.

Actually the array memory still physically exists, but it might contain garbage, which would explain the special characters.

I haven't examined the code in detail so it might be something else causing them, but that's definitely a bug which needs fixing.

Don't understand what the printf("", res) is there for - that's also a bug, but from what you wrote I think you removed it when you ran the code? I removed it from my code.

Cheers
John
3 weeks ago
The '$' is the linux console command prompt; I mentioned it in my previous post. I didn't enter it.
4 weeks ago
Bear in mind that the last string, the URL, will include the \n added by fgets().
4 weeks ago
(In case you're unfamiliar with a linux command line environment, the '$' is the linux prompt, and the './a.out' is what I entered to run the compiled program which is in file a.out (default for executables built by the gcc compiler).)
1 month ago
Not sure if I understand the question, but I think so - to clarify, this:
When I compile and run it:
You can see my input being echoed by the printf(), and it exits when I enter 'quit'. So I'm pretty sure it works; if it doesn't in zybooks then there's something we don't understand about the way zybooks is set up to handle input.
1 month ago