# Search:

Type: Posts; User: Malcolm McLean

Page 1 of 20 1

1. ## Thread: using memcpy to fill a struct

by Malcolm McLean
Replies
4
Views
125

### If parent_t contains a pointer to a child_t...

If parent_t contains a pointer to a child_t rather than an instance of a child_t, then you have to create the memory space for the child_t somehow. That can be by using malloc, or if you don't want...
2. ## Thread: Get Exponent of Float

by Malcolm McLean
Replies
16
Views
426

### So if the absolute value is greater than two you...

So if the absolute value is greater than two you repeatedly divide by two, otherwise you repeatedly multiply by two, until you have a number in the form 1.xxxxxx. The number of steps gives you the...
3. ## Thread: Get Exponent of Float

by Malcolm McLean
Replies
16
Views
426

### The answer is to use the frexpf() function to...

The answer is to use the frexpf() function to decompose a float into exponent and mantissa. Internally it will be doing bit shifting and masking and you can write your own version on similar...
4. ## Thread: Remove the extension of a file passed as a parameter (argv[1])

by Malcolm McLean
Replies
2
Views
470

### You want to work with a copy of the command line...

You want to work with a copy of the command line argument. It's
not a good idea to modify the argv[] strings in place.

So call malloc() with strlen(argv[1]) + 1 (for the nul), then strcpy....
5. ## Thread: Free 3D graphics libraries with C API

by Malcolm McLean
Replies
4
Views
697

### Open GL is the the obvious one. Getting Started...

Open GL is the the obvious one.
Getting Started - OpenGL Wiki
6. ## Thread: 2d matrix beginner.

by Malcolm McLean
Replies
8
Views
562

### Matrices can be hard to understand. It's easier...

Matrices can be hard to understand. It's easier when you think of them as something concrete rather than an abstract "matrix". When I'm teaching matrices, I use football as an example. Say you've got...
7. ## Thread: From a string, remove all the words with `n` number of vowels

by Malcolm McLean
Replies
4
Views
536

Start with the function isvowel(char ch) that tells you whether a character is a vowel or not. Then build countvowels(char *str) on top of it.
That's the logic for you program.
Now the hard part is...
8. ## Thread: How to generate a random number in the interval of -1 and 1?

by Malcolm McLean
Replies
17
Views
1,169

### define two macros. One is uniform(), which create...

define two macros. One is uniform(), which create a random number on 0-1. The other is lerp, which interpolates between a and b by paramter t, on 0-1.

So

x = lerp(-1.0, 1.0, uniform());

...
9. ## Thread: Create tar-like archiver

by Malcolm McLean
Replies
4
Views
605

### I don't think that's a good idea. It's best to...

I don't think that's a good idea. It's best to write your own tar attempt first, then worry about how the actual tar implementation does it later.

Tar collapses a directory into one file. A...

by Malcolm McLean
Replies
14
Views
810

### Computers can run out of memory. But if you are...

Computers can run out of memory. But if you are making valid allocations in a small beginner's program on a desktop machine, this is very unlikely.

You need to test the return from realloc() for...
11. ## Thread: WAV File - Summary In A Row Of Sine Waves... Need Help

by Malcolm McLean
Replies
3
Views
469

### It's really a signals processing issue rather...

It's really a signals processing issue rather than a C issue.

The body of the sound (dot dash) can be either a pure sine wave or a combination of sine waves. In a real application you would...
12. ## Thread: Detecting "letter-like" UTF-8 code points

by Malcolm McLean
Replies
15
Views
903

### UTF-8 is backwards compatible with ascii, which...

UTF-8 is backwards compatible with ascii, which is a huge advantage. UTF-16 has the disadvantage that it can't represent some code points, whilst UTF-32 is rather extravagant if most of the text is...

by Malcolm McLean
Replies
4
Views
493

### C strings are terminated by 0 (nul), not newline....

C strings are terminated by 0 (nul), not newline.

In C, strings are basically just arrays of 8 bit ascii values arranged in memory. There's jsut a little bit of special syntax to allow you to...

by Malcolm McLean
Replies
3
Views
588

### struct A has to be padded to 8 bytes so that it...

struct A has to be padded to 8 bytes so that it is correctly aligned when you set up an array of them. struct B contains an identical struct to A, plus anohter member. So it's 8 bytes, plus one byte...
15. ## Thread: Simple binary digits separator + convert to decimal

by Malcolm McLean
Replies
5
Views
528

### That is probably a mistake. You can make it work...

That is probably a mistake. You can make it work for small binary numbers, but it's not very logical and hard to error check.

If you understand strings, you are best off entering the binary as a...
16. ## Thread: How to pass multiple user arguments to a callback function?

by Malcolm McLean
Replies
12
Views
925

### When you are setting up a function pointer, you...

When you are setting up a function pointer, you should always pass back a void * supplied by caller. qsort() doesn't do this, which doesn't matter much as the function pointer is usually a trivial...
17. ## Thread: Is there any specific method to turn an array value from positive to negative?

by Malcolm McLean
Replies
2
Views
491

### If they are all non-negative, just set to the...

If they are all non-negative, just set to the negative, as Salem suggests. Note that in C, = means "assign".

If some are negative, and you want them all to be negative, then you need to test them...

by Malcolm McLean
Replies
33
Views
1,287

### You've managed to create something which is...

You've managed to create something which is correct C, but the wrong way to use pointers.

createList returns a pointer to allocated memory. So you need to hold on to that pointer to free it later....
19. ## Thread: Combinations with elements from different arrays

by Malcolm McLean
Replies
9
Views
622

### It depends if you have to handle each case with...

It depends if you have to handle each case with custom code or not. If all cases can be handled with the same code, then 480 is nothing. If you have to write separate functions for all 480 cases,...
20. ## Thread: Combinations with elements from different arrays

by Malcolm McLean
Replies
9
Views
622

### An array can be shuffled in N factorial...

An array can be shuffled in N factorial combinations. If you are selecting M elements from a set, the number of selections you can make is N!/((M!) (N-M)!).

If you are picking one element each...
21. ## Thread: How to pass multi-dim array to a function?

by Malcolm McLean
Replies
10
Views
962

### Multi-demensional arrays are often taught in...

Multi-demensional arrays are often taught in beginners' books shortly after single dimensional arrays. I'ts not normally made clear that they are an advanced feature in C. If the array has dimensions...

by Malcolm McLean
Replies
3
Views
483

### That's a brute force way of doing it. You can...

That's a brute force way of doing it.

You can construct a string to pass to system(). Use snprintf(), and test it first by outputting the command. Then write a real program that passes it to...
23. ## Thread: Bit fields and flash memory

by Malcolm McLean
Replies
2
Views
558

### Bit fields are intended to reduce memory size....

Bit fields are intended to reduce memory size. That's one of their main purposes. The other reason is to get a specific bit layout in memory for talking to hardware.

24. ## Thread: Help with old exam project

by Malcolm McLean
Replies
1
Views
469

### You need an array of structures from part one....

You need an array of structures from part one. These have fields including doctor and date (is that surname? It's not clear to me from the format where the doctor is stored).

You also need to...
25. ## Thread: Printing ordered data from file

by Malcolm McLean
Replies
1
Views
560

### The best place to start is with your data...

The best place to start is with your data structures.
Here it's quite simple. You need a struct with a single key / value pair, both strings. It probably mkaes sense to make them both char *s and...
Results 1 to 25 of 496
Page 1 of 20 1