Quote:
> This is a headache for me at the moment. I am beginning to get the feeling
> that all data must be entered in main to get the right results.
Well, this is obviously not necessary. But if you don't want the data
to be processed in their entirety in functions being called from the
function in which you enter them, you have two options. Either store
them globally (rarely a good idea), or follow one of the methods
described in this thread for returning them from a function. As I said,
I favor forgetting about using the return value, and letting the calling
function pass the address of the array to receive the data as an
argument. This is how most of the functions in the standard library
treat strings, which are really arrays of char.
Quote:
> I am trying to get the hang of pointers and arrays, unfortunately it
> hasn't happened yet. I have tried both the static and dynamic route. I
> either get segmentation faults or parse error. I tinkered with a suggested
> implimentation as follows:
> static int *Getdata(int *hbig)
> {
> int i,big;
> int *array;
> printf("\n How big is the array?\n");
> scanf("%d\n", big);
I generally shy away from using "scanf" to read values from stdin, as it
is very inferior in handling incorrect user input. It is better to read
input to a string with a function such as "fgets" (but do *not* ever use
gets), and then passing the resulting string to sscanf later.
Quote:
> hbig = &big;
This will undoubtably *not* do what you want, though it is legal code,
and therefore will not produce an error from your compiler. What you
are doing here is passing the *address* of the local variable "big" to
"hbig." In so doing, you lose the address that was stored in hbig to
begin with, which was presumably the address of an outside variable in
which you intended to store the *value* of "big." As it is, you have
lost any way for the "Getdata" function to access that variable.
Nothing will be stored there.
Presumably, what you intended was:
*hbig = big;
While "hbig" is a pointer, the expression "*hbig" refers to whatever
variable "hbig" points to -- presumably an outside variable that passed
its address to "Getdata." Therefore, the above statement assigns the
value of "big" to that variable.
Quote:
> int *array = (int *)malloc((big)*sizeof(int));
^^^^^
The "int *" part in the beginning of this statement makes it look like a
variable definition, which in C must be either at the beginning of a
block (enclosed with { ... }) or outside any function whatsoever. In
this case, it is not necessary at all, since you *already* defined
"array" earlier in the function. Also, the (int *) cast in front of
"malloc" is unnecessary, and might prevent the compiler from emitting
helpful warning messages. Neither is there any need to enclose "big" in
parentheses. So:
array = malloc(big*sizeof(int));
You could also have a check to see if malloc returned NULL.
Quote:
> for (i=0; i<big;i++) {
> printf("\n Enter your data \n");
> scanf("%d", array[i]);
> }
Again, the use of "scanf" is problematic if the user makes a mistake.
For instance, if he accidentally types a letter, "scanf" will return
without storing any value, but the letter would remain unread for the
next turn. The loop would run through the entire remainder of "array"
without letting the user type any more input, and without storing
anything.
Quote:
> return array;
> }
And as I said before, you have left it to the caller to "free" the
returned array after it has been used, which is a little awkward.
Quote:
> This gives me a parse error before int *array = (int*) malloc...etc.
> I don't get it. Is it really this hard to pass data between functions?
You are hit by C's lack of "call-by-reference." *All* function
arguments in C are passed by value, meaning that functions only work on
a local copy rather than the variable used in the calling function
(although arrays can give the illusion of being passed by reference).
The workaround is to use pointers as arguments. Therefore, it is pretty
much *essential* to learn how to use pointers properly in C.
I know well how pointers work, and the common ways to pass arguments.
Once this knowledge is in, it gets easier to understand.
Quote:
> I really want to be able to pass structures but I haven't been able to
> pass data from one function to another. Are there any good books above
> "for dummies" and below "C problem solving &programming-KA barclay" ?
> I have about 6 months of c programming experience and am now really trying
> to get a more in depth understanding more or less.
I share the common opinion that the best introduction to C is the book
"The C Programming Language" by Kernighan & Ritchie. It was *my*
introduction to the language. It assumes some basic understanding of
programming in general, though. However, it has a full section
dedicated to explaining the use of pointers.
I have never read the book "C For Dummies," but what I've heard,
suggests to me that it teaches some bad practices. Part of my opinion
is based on reading a post from someone who claimed to have read it from
beginning to end, and still proved to understand nothing about either
function arguments or pointers.
And by all means, stay away from anything written by Herbert Schildt.
Yngvar
--