Pointer Confusion: Solve the Mystery

  • Thread starter martix
  • Start date
  • Tags
    Confusion
In summary: YoungIn summary, the conversation discusses issues with the code, such as incorrect variable names, syntax errors, and incorrect use of pointers. The person asking for help is struggling with understanding how to properly assign values and access elements in the code. They are seeking assistance in resolving these issues.
  • #1
martix
169
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
  • #2
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
 
  • #3


It seems like there may be some confusion with how pointers are being used in this code. The use of starr and stpt as pointers to an array and start of an array respectively can be confusing. Additionally, the syntax error with the malloc function and the error with accessing the elements of the starr array suggest that there may be issues with how the pointers are being dereferenced.

To solve this mystery, it may be helpful to review the basics of pointers and their syntax in C. Additionally, it may be useful to use a debugger or print statements to track the values of the pointers and see where the errors are occurring. It may also be helpful to carefully review the purpose and intended use of each pointer in the code.

In general, pointer confusion can be a common issue in programming, but with careful attention to detail and a solid understanding of pointer syntax, it can be solved.
 

FAQ: Pointer Confusion: Solve the Mystery

What is "Pointer Confusion: Solve the Mystery"?

"Pointer Confusion: Solve the Mystery" is a game that challenges players to use their problem-solving skills to decipher clues and solve a mystery involving pointers, a common concept in computer science.

Who can play "Pointer Confusion: Solve the Mystery"?

Anyone with an interest in computer science and problem-solving can play "Pointer Confusion: Solve the Mystery". It is suitable for all ages and does not require any prior knowledge of pointers.

How is "Pointer Confusion: Solve the Mystery" helpful for learning about pointers?

By playing "Pointer Confusion: Solve the Mystery", players can apply their knowledge of pointers in a fun and interactive way. The game presents challenges that require understanding of pointers, helping players to solidify their understanding of the concept.

Are there different levels of difficulty in "Pointer Confusion: Solve the Mystery"?

Yes, "Pointer Confusion: Solve the Mystery" has different levels of difficulty to cater to players with varying levels of knowledge and experience with pointers. The difficulty increases as players progress through the game.

Can "Pointer Confusion: Solve the Mystery" be played solo or with others?

"Pointer Confusion: Solve the Mystery" can be played both solo and with others. Players can challenge themselves by playing alone or collaborate with friends to solve the mystery together.

Similar threads

Replies
6
Views
7K
Replies
75
Views
5K
Replies
16
Views
4K
Replies
1
Views
2K
Replies
4
Views
5K
Replies
49
Views
10K
Back
Top