Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trivial char/string/malloc question

Status
Not open for further replies.

mrkan

Programmer
Oct 30, 2006
39
US
I am beginner... can somebody help me/or explain how to do this. I am struggling with pointers...

In main program I declare array of characters (or pointer to array)...

Then I have function to which I pass size of the array. I malloc the space and then I populate array.
I return success or false (depend of malloc) and in main part I print the whole array.

Here is my pseudo code:
Code:
int main {
...
char *myarray;
int size = 2;
int result = populateArr(size, myarray);
for (n = 0; n < 3; n++)
    printf("%s", myarray);
...
}


int populateArr(int size, myarray) {
    myarray = malloc(size * sizeof(char))
    *myarray="a";
    *myarray++;
    *myarray="b"; 
    myarray = realloc(myarray, size + 1 * sizeof(char))
    *myarray++;
    *myarray="c"; 
}

It's a junk, but I think you will get idea what I want to learn...

Thanks
 
1. To return a pointer you must pass a pointer to a pointer:
Code:
int populateArr(int size, char** p2arr) /* all parms types*/
{
    char* p = malloc(size); /* sizeof(char) == 1 in C */
    ...
    *p2arr = p;
}
...
char* myarray;
...
int result = populateArr(size, & myarray); /* not myarray */

2. *myarray = "a"; Well, in C "a" is an array of char, so it implicitly converted to a pointer to char. Now you try to assign a pointer to a char - nonsense. It's 'a' denotes a char value, not "a"...

3. This exercise is a good sample of a very dangerous programming style (allocate memory deeply in called routine then return a pointer then forget to deallocate and so on;)...

4. In main program I declare array of characters (or pointer to array)...
An array and a pointer to an array are different animals...
 
To elaborate on ArkM's first point. Think of it like this - unless you pass a reference (C++ only I believe), you can only pass info to functions by value. So when you pass a pointer to a function, the function is making a local copy of the pointer, but of course when you de-reference the pointer, it's still pointing to the same memory location inside the function as it did outside the function. If you assign a different address to the pointer inside the function, you are only changing the local copy of the pointer (the pointer outside the function is a different variable and therefore is unchanged).
If you pass a pointer to a pointer (ex. char**) then you can dereference it once to get the real pointer to the data, and if you modify the dereferenced pointer, it will modify it outside the function also, since both are pointing to the same location on the heap.
 
thanks both... I have to spend some quality time over this...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top