Pointer Confusion: Solve the Mystery

  • Thread starter Thread starter martix
  • Start date Start date
  • Tags Tags
    Confusion
Click For Summary
SUMMARY

The discussion centers on pointer confusion in C programming, specifically regarding the allocation and manipulation of pointers and structures. The user encounters syntax errors and type conversion issues when using malloc and accessing structure members. Key points include the need to properly cast the void pointer returned by malloc and the distinction between pointers and structures. Warren provides definitive solutions, emphasizing the importance of meaningful variable names and correct usage of dot-notation versus arrow-notation for structure access.

PREREQUISITES
  • Understanding of C programming language syntax
  • Familiarity with dynamic memory allocation using malloc
  • Knowledge of pointers and structures in C
  • Experience with debugging common C errors
NEXT STEPS
  • Research proper usage of malloc and memory management in C
  • Learn about pointer arithmetic and structure manipulation in C
  • Explore debugging techniques for C programming errors
  • Study best practices for naming conventions in programming
USEFUL FOR

C programmers, software developers, and students learning about memory management and pointer manipulation in C. This discussion is particularly beneficial for those facing challenges with dynamic memory allocation and structure access in their code.

martix
Messages
167
Reaction score
5
There something really fishy going on here and I can't put my finger on it...
Here is the situation in condensed form:

Code:
	WordSt **stpt, *starr; //stpt - pointer to start of array; starr - array of pointers to the structs
...
		stpt=malloc(count*sizeof(starr*)); //syntax error : ')' - what's 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:
Technology news on Phys.org
There are so many things wrong here, it's hard to even know where to begin.

1) Give your variables meaningful names.
2) stpt is not a pointer, it's a pointer to a pointer. malloc returns a pointer, so you don't want to be assigning the result of malloc to stpt.
3) The syntax "starr*" is meaningless in the context you used it.
4) You're assigning values to starr[wz], but starr is a garbage pointer, and this will cause a segfault.
5) You need to cast the void* that malloc returns into the WordSt* you want it to be.
6) starr[wz] is a structure, not a pointer to a structure, so you should not be using ->, you should just be using dot-notation. You don't need to dereference starr[wz] at all, as starr[wz] is not a pointer. That's what "illegal indirection" means.

- Warren
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
8K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 13 ·
Replies
13
Views
21K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 49 ·
2
Replies
49
Views
12K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
2K