Copyright 2004 by M. Uli Kusterer Tue, 30 Dec 1969 07:58:58 GMT Comments on article book7 at Zathras.de http://www.zathras.de/angelweb/book7.htm book7 Comments witness_dot_of_dot_teachtext_at_gmx_dot_net (M. Uli Kusterer) witness_dot_of_dot_teachtext_at_gmx_dot_net (M. Uli Kusterer) en-us Comment 15 by Joshua http://masters-of-the-void.com/book7.htm#comment15 http://masters-of-the-void.com/book7.htm#comment15 For the last example on the last paragraph on the main() function, how would argc be assigned the value 3? And what is the purpose of making a pointer a pointer? Sorry, while I get the Array (was once a programmer many moons ago), I don't get this example. Much appreciate any help.
Comment 14 by Aman http://masters-of-the-void.com/book7.htm#comment14 http://masters-of-the-void.com/book7.htm#comment14 Please i dont understand why you said that the following wont work:


int numItems;
printf( "Please tell me how many items you will need at most:" );
scanf( "%d", &numItems );
fpurge( stdin );
int myIntArray[numItems]; // *** This line won't work. ***

My whole program is based on this and it works.....as you can see i ask the user to input the number of entries required in the database, and then using that i size up the database...am i missing something?



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Database {
char name[40];
int age;
char occup[40];
};

int main ()
{

printf("\n\n\nNow that the test session is over, our first small program starts!!\n\n\n\n");
int i=0;
int entries;
printf("Please enter the number of database entries you would like to have:");
scanf("%d", &entries);
fpurge(stdin);
// int* entriespointer = malloc(entries);

struct Database movies[entries];
while (i!=(entries)) {
printf("Name:");
scanf("%39s", movies[i].name);
fpurge(stdin);
printf("\nAge:");
scanf("%d", &movies[i].age);
fpurge(stdin);
printf("\nOccupation:");
scanf("%39s", movies[i].occup);
fpurge(stdin);
i++;

}


i=0;
while (i!=(entries)) {
printf("\n\nName is %39s", movies[i].name);
printf("\nAge is %d", movies[i].age);
printf("\nOccupation is %39s", movies[i].occup);
i=i++;
}
}
Comment 13 by Richard Howell http://masters-of-the-void.com/book7.htm#comment13 http://masters-of-the-void.com/book7.htm#comment13 void fakeArray()
{
int * memAlArray = malloc(sizeof(char[4]) * 2);

memAlArray[0] = "One";
(!assignment makes integer from pointer without a caste)
memAlArray[1] = "Two";
(!assignment makes integer from pointer without a caste)
memAlArray[2] = "Three";
(!assignment makes integer from pointer without a caste)

printf("\n Position 1 > %s Position 2 > %s Position 3 > %s ",
memAlArray[1], memAlArray[2], memAlArray[3]);
(!format %s expects type char *, but argument 2 has type int)
}

OK - I tried to put strings into a malloc space, and these are the errors I got (above) can anyone enlighten me?
Comment 12 by icebalm http://masters-of-the-void.com/book7.htm#comment12 http://masters-of-the-void.com/book7.htm#comment12 The reason you want to test if (myLargerText != 0) is to see if the realloc() succeeded or not. If realloc() fails for any reason, such as your computer ran out of memory, it will return NULL or for our purposes here, 0.

If you tried to start performing operations on myLargerText after a failed realloc(), then myLargerText would be a pointer to nothing, and at the very least your program would crash. When working with pointers to memory locations it is extremely important to do proper error checking like in this example!
Comment 11 by Charles Marshall http://masters-of-the-void.com/book7.htm#comment11 http://masters-of-the-void.com/book7.htm#comment11 I'm confused as to the reason for the if/else statement in the last example:
if(myLargerText!=0), etc. Why is that needed? Would it not be just as easy to rewrite/redefine the original array instead of using realloc()? I guess I'm missing the big picture here.
Comment 10 by Charles Marshall http://masters-of-the-void.com/book7.htm#comment10 http://masters-of-the-void.com/book7.htm#comment10 I have a similar question to John Dunson's regarding accessing a variable within an array. If:

int elementSix;
elementSix = myIntArray[5];

puts the value of myIntArray[5] into "elementSix", would just typing "myIntArray[5]" by itself do the same thing or would it try to create another myIntArray with space for 5 ints?
Comment 9 by Uli Kusterer http://masters-of-the-void.com/book7.htm#comment9 http://masters-of-the-void.com/book7.htm#comment9 Uli Kusterer writes:
FatalMojo, C and C++ have some notable differences. One of them is that C++ is even more picky when it comes to implicitly converting between types (like from void* to int*, which C lets you get away with).

As to the temporary variable: Your code only works as long as nothing goes wrong during the realloc(). If, for example, your computer runs out of memory, realloc() will return NULL, and leave the original memory that myText pointed to untouched. But since you assign the result of realloc() to myText, you're left with myText being NULL, and you don't have a pointer to the old memory block anymore. You just leaked a memory block and lost its address.
Comment 8 by FatalMojo http://masters-of-the-void.com/book7.htm#comment8 http://masters-of-the-void.com/book7.htm#comment8 So I'm wondering, would it be for clarity that the example was written as such:


char* myText = malloc( 2 * sizeof(char) );
char* myLargerText = 0;

...

myLargerText = realloc( myText, 3 * sizeof(char) );


where we need to declare two variables initially to be able to resize our dynamic array as opposed to:


char* myText = malloc( 2 * sizeof(char) );

...

myText = realloc(myText, 3 * sizeof(char) );


where the original memory pointer is being resized without the need to define a "holder" variable?
Comment 7 by FatalMojo http://masters-of-the-void.com/book7.htm#comment7 http://masters-of-the-void.com/book7.htm#comment7 Okay, so I did a google search with my error and I fixed it, basically, I was using a C++ project whereas this tutorial is for C, and it would seem that either malloc() is different in C and C++, or that void is not the same in C++ as it is in C.

Any insight anyone?
Comment 6 by FatalMojo http://masters-of-the-void.com/book7.htm#comment6 http://masters-of-the-void.com/book7.htm#comment6 I was very excited with this article, however, when trying it out, i'm getting a:

"error: invalid conversion from 'void*' to 'int*' "

from the compiler. At first, I thought I made a mistake somewhere and tried to isolate it, and when I ran out of ideas, I decided to test the code copy and paste this code directly from the site:

#include <stdlib.h>

int main()
{
int* myArray = malloc( sizeof(int) * 20 ); // could write int myArray[20]; instead.

myArray[5] = 42;

return 0;
}

But I do get the exact same error. Any ideas?
Comment 5 by Uli Kusterer http://masters-of-the-void.com/book7.htm#comment5 http://masters-of-the-void.com/book7.htm#comment5 Uli Kusterer writes:
Mk12: The computer has to do all this pointer and memory stuff internally, for every variable (see description of the stack in earlier chapters). Usually it just remembers the address of the variable. For an array, it has to do the maths described here to turn an index into the actual address. If you write

myArray[x]

and myArray is an array of ints, it won't know which item you want until you actually run the program and write a value in the variable x, so it needs to remember the first item's address, and then do

firstItemAddress +(sizeof(int) * x)

to determine what item you are really talking about. It can't keep a list of all addresses, as that would require already having an array, which would need a list of addresses, which would need an array ... chicken and egg problem. Since they need the address anyway, they just decided to always remember an array by the pointer, the address of its first item, as that's the most flexible way to get at all items.
Comment 4 by anon http://masters-of-the-void.com/book7.htm#comment4 http://masters-of-the-void.com/book7.htm#comment4 Suggestion to clarify: add a few comments as I have below.

int* myArray = malloc(1); // allocates 1 byte myArray
myArray[0] = 700; // and int is more than 1 byte, you only assigned one byte above.
Comment 3 by Mk12 http://masters-of-the-void.com/book7.htm#comment3 http://masters-of-the-void.com/book7.htm#comment3 Why do the arrays need to be pointers?
Comment 2 by Uli Kusterer http://masters-of-the-void.com/book7.htm#comment2 http://masters-of-the-void.com/book7.htm#comment2 Uli Kusterer writes:
John, one reason would be if you wanted to change myIntArray, but still keep its old value around for other uses. E.g. when you want to swap two values in an array, you need to remember one variable's value, replace its value, then take that old value and put it in the other variable.
Comment 1 by John David Dunson http://masters-of-the-void.com/book7.htm#comment1 http://masters-of-the-void.com/book7.htm#comment1 why would you ever use a variable to hold the value of another variable:

int elementSix;
elementSix = myIntArray[5];

couldn't you use myIntArray[5] in any place that you could use elementSix?