[C] Stupid pointers what the heck is going on here

It all makes sense now.In summary, The speaker has been struggling to use strcmp to compare a character to a string stored in a char** pointer. They eventually realize that the issue is with using a character instead of a string, and that "a" should be used instead of 'a' for strcmp to work properly.
  • #1
zeion
466
1
Hi,

I don't understand how this can be so difficult.
So I have a [char ** ptr] pointer that stores a list of arguments.
I'm able to PRINT each of them by doing printf("%s\n", ptr)
But how the heck do I is strcmp to see if a certain argument is there?
Apparently I cannot just do [if (strcmp(ptr, 'a') == 0].
I don't understand what strcmp needs for it to work. Do I need one star? Two stars? Five stars? Why is this so hard?

Thanks
 
Technology news on Phys.org
  • #2
strcmp compares strings (via char* pointers). Your problem is not with ptr but with 'a' which is not a string, but a character. "a" is a string (consisting of one character 'a').
 
  • #3
DrGreg said:
strcmp compares strings (via char* pointers). Your problem is not with ptr but with 'a' which is not a string, but a character. "a" is a string (consisting of one character 'a').


Nooo wayyy I've been trying to debug that the whole day!
 

Related to [C] Stupid pointers what the heck is going on here

1. What are pointers in C and why are they important?

Pointers in C are variables that store the memory address of another variable. They are important because they allow for dynamic memory allocation and efficient manipulation of data structures.

2. How do I declare and initialize a pointer in C?

To declare a pointer in C, use the * symbol before the variable name. For example, int *ptr; To initialize the pointer, assign it the address of a variable using the & symbol. For example, ptr = # where num is an integer variable.

3. How do I use pointers to access and modify data in C?

To access the data stored in a variable using a pointer, use the * symbol before the pointer variable. For example, *ptr will give you the value stored at the memory address stored in the pointer variable. To modify the data, simply assign a new value to *ptr. For example, *ptr = 10; will change the value of the variable stored at the memory address to 10.

4. What are the common errors associated with pointers in C?

Some common errors associated with pointers in C include dereferencing a null pointer, which can cause a segmentation fault, and not properly allocating memory for a pointer, which can lead to memory leaks. It is important to properly initialize and allocate memory for pointers to avoid these errors.

5. How can I use pointers to create and manipulate data structures in C?

Pointers are commonly used in C to create and manipulate data structures such as arrays, linked lists, and trees. By using pointers, you can dynamically allocate memory for these data structures and easily traverse and modify them. Pointers are also useful for passing these data structures as arguments to functions, allowing for more efficient and flexible code.

Similar threads

  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
4
Replies
118
Views
7K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
19
Views
4K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
4
Views
766
  • Programming and Computer Science
Replies
2
Views
11K
Back
Top