Pointer confusion

Apr 4, 2008 at 12:45pm
There something really fishy going on here and I can't put my finger on it...
Here is the situation in condensed form:

1
2
3
4
5
6
	WordSt **stpt, *starr; //stpt - pointer to start of array; starr - array of pointers to the structs
...
		stpt=malloc(count*sizeof(starr*)); //syntax error : ')' - whats with that anyway?
		....
			starr[wz]=malloc(sizeof(WordSt)+len*sizeof(char)); //C2679: binary '=' : no operator found which takes a right-hand operand of type 'void *' (or there is no acceptable conversion) Trying to convert it doesn't cut it either...
			starr[wz]->len=len; //Says it need '.' When I put '.'(as in (*starr[wz]).len) it says "illegal indirection". Also any version with -> doesn't work 

What I know - indexing is also a form of memory manipulation, so it should work. And (*b).el == b->el. Btw first option gives me an automatic menu for choosing between the elements, no such luck with the second one.
Help!
Last edited on Apr 4, 2008 at 12:45pm
Apr 4, 2008 at 1:09pm
first of all...
why do u use sizeof(starr*) in malloc statement...?
did u want to use sizeof(starr) if its just to get the size of one pointer variable!!!

And now, malloc returns a void pointer... so starr has to be "pointer to a pointer", if u intend to use it in above fashion.

Apr 4, 2008 at 1:54pm
sizeof(starr*) as in the size of one of the members in the array. Count being the number of members, together giving me all the required space for the array...
So what we have is -
**stpt - points to the beginning of the array of pointers
*starr - this an array of pointers, each pointing to the beginning of a struct of type "WordSt".
First I count how much structures I will have and allocate memory accordingly to accommodate pointers to all structs. The first malloc does that and sets the stpt var.
Then I begin my loop for allocating memory for each struct and assigning the returned pointer to its according position in the starr array. Then I begin (trying) to populate each struct by referring to its pointer(since they have no names).

And it fails. And I want it to succeed and you have to tell me how since all my attempts to make it work have been futile.
Apr 4, 2008 at 2:39pm
Yes, it is a syntax error as, starr* , does not state any thing clearly what you mean to, and it is wrongly coded.

You can supply a type to find the size of it, as you did in next line for char type.

If you want to find out the size of the array it pointed, give the size of that array (specifying the name, but not by a pointer).

For example,
starr = &myArray[0].

give:
sizeof(myArray); then it would give you the size of myArray.
If it has 10 elements which each is a pointer, then the result would be 40 as each pointer holds a 4 byte amount of memory.

Check it out. Good luck :)
Apr 4, 2008 at 3:34pm
Absolute bullshit! Everything thus far(incl. my code).
satm2008 - sizeof(starr*) means the size of the type of the starr* variable. Or at least if should mean that.
And why would I need to find the size of the array? How can I know what to write in the [] if I don't know it. And why would I need this if I knew it anyway? Which is basically to say - what you suggested doesn't do anything.
But I believe I found the error in the logic.
Which is - I need only one variable...
So in the end it give me something like this:
stpt - containing the address of the start of the array
(*stpt)[i] - the i-th member of the pointer array.
(*(*stpt)[i]).el OR ((*stpt)[i])->el - being the el element of the i-th struct pointed by the i-th member of the array.

Correct me if I'm wrong, I don't have the ability to check it right now.
Last edited on Apr 4, 2008 at 3:44pm
Apr 4, 2008 at 6:52pm
Buddy, what I meant to say is that you should be specific when ask malloc() to find allocate an amount you require.
As I mentioned it earlier, sizeof(starr*) obviously give you error as it is a syntax error, instead, it should be sizeof(starr)

And your first note says *starr is an "array of pointers" while your code shows it as just a pointer.
To justify your note, starr should be declared as an array of pointers, noting that array is of a fixed size.

Lets say, to hold 10 pointer elements into an array, you would declare,

WordSt *starr[10]; //array of 10 pointers of WordSt type.

for (ndx = 0; ndx < 10; ndx++)
{
starr[ndx] = (WordSt *) malloc(sizeof(WordSt)+len*sizeof(char)); // should work now
// ...ada ada ada
}

//now point to it through stpt, which is a pointer to pointer

stpt = (WordSt **) malloc(count*sizeof(starr)); // note there is no * after starr
stpt[0] = &starr[0]; // stores address of the first pointer in starr into stpt's first element, mening, it is a "pointer to pointer"
...


The sequence should go like that. Compare it with your existing code how it was code and check it with this tip.

Good luck :)







Apr 7, 2008 at 2:01pm
hey martix,

1. sizeof(starr*) will give NOT u size of the array!! The usage is faulty.
2. Malloc returns a void pointer... Where is the "type conversion" for the pointer????

This was to clarify my concerns which i posted earlier.

I wont comment further as i m not sure how u defined "WordSt".... I hope u defined it in a manner that it can store addresses!!! DONT Forget that malloc function returns an ADDRESS.


Topic archived. No new replies allowed.