Newbie Question: how to interpret library info?

M

myk

Hi. I'm a Matlab user trying for the first time to use C. Simple
things are familiar, more complex are not. I've been online looking for
good tutorials but they usually address simple things or complex things
I don't need to know at the moment.

There is a function I wish to call from the bzip2 library:
ftp://sources.redhat.com/pub/bzip2/docs/manual_3.html#SEC36. Basically,
I don't understand how to interpret what I'm reading. For example:

a) how do I call the function e.g. "compress_out = int
BZ2_bzBuffToBuffCompress(....)" , or just "int BZ2_bzBuffToBuffCompress)"?
b) the first argument is " char* dest", but if dest is a pointer how
can it be a character?

In a nutshell, I know what the function will do for me but how do I
interpret how to use it from the available documentation? Thanks for
any assistance you can provide.

m
 
A

Allin Cottrell

myk said:
Hi. I'm a Matlab user trying for the first time to use C. Simple
things are familiar, more complex are not. I've been online looking for
good tutorials but they usually address simple things or complex things
I don't need to know at the moment.

There is a function I wish to call from the bzip2 library:
ftp://sources.redhat.com/pub/bzip2/docs/manual_3.html#SEC36. Basically,
I don't understand how to interpret what I'm reading. For example:

a) how do I call the function e.g. "compress_out = int
BZ2_bzBuffToBuffCompress(....)" , or just "int BZ2_bzBuffToBuffCompress)"?
b) the first argument is " char* dest", but if dest is a pointer how
can it be a character?

The prototype for this function is

int BZ2_bzBuffToBuffCompress(char* dest,
unsigned int* destLen,
char* source,
unsigned int sourceLen,
int blockSize100k,
int verbosity,
int workFactor );

meaning that it returns an int. As the documentation explains, the int
returned is in the nature of an error code. if the value is not
BZ_OK (a macro whose actual numeric value we don't need to know), then
something has gone wrong.

A call might look like:

int err;

err = BZ2_bzBuffToBuffCompress(...);
if (err != BZ_OK) {
/* handle the error */
;
}

You ask: "the first argument is " char* dest", but if dest is a pointer
how can it be a character?"

This is a _very_ basic question. If you want to mess with a C-style
API, you should read some basics on C (e.g. Kernighan and Ritchie,
second ed.). A pointer is a variable that points to, or holds the
address of, an area of memory. A pointer-to-char (char *) is a pointer
whose target memory contains char data. In the above, "dest" is a
pointer, not a character. It seems to be the caller's
responsibility to ensure that the dest buffer is big enough to
contain the compressed data without overflowing.

Allin Cottrell
 
M

mykbourassa

Thanks very much for the reply!
You ask: "the first argument is " char* dest", but if dest is a
pointer how can it be a character?"

This is a _very_ basic question. If you want to mess with a C-style
API, you should read some basics on C (e.g. Kernighan and Ritchie,
second ed.). A pointer is a variable that points to, or holds the
address of, an area of memory. A pointer-to-char (char *) is a pointer
whose target memory contains char data. In the above, "dest" is a
pointer, not a character.


This is what has me confused (well one thing). The dest points to
memory that does _not_ necessarily contain char data. It can be
anything since it's a zip function and of this I am certain. So what
happens then? I've just come across 'cast' that seems to promise a
solution but
I've been doing C for less than four nights using Dietel^2.

m
 
J

Joe Wright

Thanks very much for the reply!




This is what has me confused (well one thing). The dest points to
memory that does _not_ necessarily contain char data. It can be
anything since it's a zip function and of this I am certain. So what
happens then? I've just come across 'cast' that seems to promise a
solution but
I've been doing C for less than four nights using Dietel^2.

m
Try this ..

char *dest;

... defines dest a pointer to char, not a char.

char ch = 'Z';

... defines ch a char and initializes it to the character 'Z'.

dest = &ch;

... assigns the address of ch to dest. Now, expressing *dest in 'rvalue'
context you get ch or 'Z'. In lvalue context ..

*dest = 'A';

... will change ch from 'Z' to 'A'.

Dietel^2 is reputed to be good. Read more of it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top