Incompatible pointer type error using structs
c++ pointers struct
I am new to programming. I am trying to learn C and pointers, but it is giving me much trouble. I got the following error trying to implement a singly linked list. I searched online, and I couldn't find someone who had an error just like mine, or perhaps I just didn't couldn't make sense of it with my problem.
The following is the error I received:
warning: incompatible pointer types initializing 'NODE *' (aka 'struct node *') with an expression of type 'struct NODE ' [-Wincompatible-pointer-types] NODE temp = (*l)->head;
In main, I passed the address of the variable of type LIST. So, I thought I had to dereference 'l', to get the address of where the LIST type is located, then I had to dereference with an arrow to get the address of where the NODE is located. Where am I confused? I do appreciate the help.
Below you will see the code I have written:
I guess your problem is here :
just remove struct keyword before NODE
Also you need to initialize the head with NULL to make this condition to wwork
so when you create your list add l->head = NULL;
And the last one (i hope) when you create your first node, you forget to assign head to it, and return in order not to add the first element twice
And BTW, don't cast malloc results in C
C++ – what are the differences between a pointer variable and a reference variable in c++.
A pointer can be re-assigned:
A reference cannot be re-bound, and must be bound at initialization:
A pointer variable has its own identity: a distinct, visible memory address that can be taken with the unary & operator and a certain amount of space that can be measured with the sizeof operator. Using those operators on a reference returns a value corresponding to whatever the reference is bound to; the reference’s own address and size are invisible. Since the reference assumes the identity of the original variable in this way, it is convenient to think of a reference as another name for the same variable.
You can have arbitrarily nested pointers to pointers offering extra levels of indirection. References only offer one level of indirection.
A pointer can be assigned nullptr , whereas a reference must be bound to an existing object. If you try hard enough, you can bind a reference to nullptr , but this is undefined and will not behave consistently.
You can, however, have a reference to a pointer whose value is nullptr .
Pointers can iterate over an array; you can use ++ to go to the next item that a pointer is pointing to, and + 4 to go to the 5th element. This is no matter what size the object is that the pointer points to.
A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly. A pointer to a class/struct uses -> to access its members whereas a reference uses a . .
References cannot be put into an array, whereas pointers can be (Mentioned by user @litb)
Const references can be bound to temporaries. Pointers cannot (not without some indirection):
This makes const & more convenient to use in argument lists and so forth.
C++ – a smart pointer and when should I use one
This answer is rather old, and so describes what was 'good' at the time, which was smart pointers provided by the Boost library. Since C++11, the standard library has provided sufficient smart pointers types, and so you should favour the use of std::unique_ptr , std::shared_ptr and std::weak_ptr .
There was also std::auto_ptr . It was very much like a scoped pointer, except that it also had the "special" dangerous ability to be copied — which also unexpectedly transfers ownership. It was deprecated in C++11 and removed in C++17 , so you shouldn't use it.
A smart pointer is a class that wraps a 'raw' (or 'bare') C++ pointer, to manage the lifetime of the object being pointed to. There is no single smart pointer type, but all of them try to abstract a raw pointer in a practical way.
Smart pointers should be preferred over raw pointers. If you feel you need to use pointers (first consider if you really do), you would normally want to use a smart pointer as this can alleviate many of the problems with raw pointers, mainly forgetting to delete the object and leaking memory.
With raw pointers, the programmer has to explicitly destroy the object when it is no longer useful.
A smart pointer by comparison defines a policy as to when the object is destroyed. You still have to create the object, but you no longer have to worry about destroying it.
The simplest policy in use involves the scope of the smart pointer wrapper object, such as implemented by boost::scoped_ptr or std::unique_ptr .
Note that std::unique_ptr instances cannot be copied. This prevents the pointer from being deleted multiple times (incorrectly). You can, however, pass references to it around to other functions you call.
std::unique_ptr s are useful when you want to tie the lifetime of the object to a particular block of code, or if you embedded it as member data inside another object, the lifetime of that other object. The object exists until the containing block of code is exited, or until the containing object is itself destroyed.
A more complex smart pointer policy involves reference counting the pointer. This does allow the pointer to be copied. When the last "reference" to the object is destroyed, the object is deleted. This policy is implemented by boost::shared_ptr and std::shared_ptr .
Reference counted pointers are very useful when the lifetime of your object is much more complicated, and is not tied directly to a particular section of code or to another object.
There is one drawback to reference counted pointers — the possibility of creating a dangling reference:
Another possibility is creating circular references:
To work around this problem, both Boost and C++11 have defined a weak_ptr to define a weak (uncounted) reference to a shared_ptr .
- C++ – use a pointer rather than the object itself
- | New Account
- | Log In Remember [x]
- | Forgot Password Login: [x]
- Format For Printing
- - XML
- - Clone This Bug
- - Top of page
[Solved]-how to fix problem with initialization from incompatible pointer type-C
error: initialization from incompatible pointer type [-werror=incompatible-pointer-types] .read = read_proc, error: initialization from incompatible pointer type [-werror=incompatible-pointer-types] .write = write_proc,
the signature expected of the read function is
the signature expected of the write function is
( https://linux-kernel-labs.github.io/refs/heads/master/labs/device_drivers.html )
the the names of the functions and of their parameters do not matter, but the return type and the number, order, and types of the arguments all do.
your read_proc() doesn't appear to match at all. i guess its possible that it's closer to right for some other version of the kernel than the doc i linked describes. your write_proc() function is closer, but unsigned long is probably not the same type as size_t , and void * is probably not the same type as loff_t * . either one of those discrepancies is enough for a pointer type mismatch, and they could cause genuine behavioral issues.
error: dereferencing pointer to incomplete type ‘struct proc_dir_entry’ our_proc_file->read_proc = read_proc;
it is not necessary to have the definition of struct proc_dir_entry to declare a pointer to that type, but it is necessary to have a definition in order to access members of an instance, via such a pointer or otherwise. at the point where you attempt to do
, no such definition is in scope. possibly you have forgotten to include the appropriate header. alternatively, you may have misspelled the structure tag.
- how to fix problem with initialization from incompatible pointer type
- How to fix passing argument 1 of 'count' from incompatible pointer type
- How to resolve initialization from incompatible pointer type
- Initialization from incompatible pointer type warning with an array allocated on stack
- typedef function pointer -> initialization from incompatible pointer type
- C (gcc) warning: initialization from incompatible pointer type when calling pthread_cleanup_push()
- incompatible integer to pointer conversion returning 'int' from a function with result type 'void *'
- initialization from incompatible pointer type during array initialization
- Getting context on initialization from incompatible pointer type
- proc_dir_entry warning: initialization from incompatible pointer type [enabled by default]
- Initialization from incompatible pointer type when assigning struct file_operations fields
- How to place string into an array in c. Also why do i get return from incompatible pointer type error?
- initialization of 'const char **' from incompatible pointer type 'char *'
- Function pointers - outside library, warning: initialization from incompatible pointer type
- How to return a pointer with a type from a void pointer, when the type is known
- Pointer that points to return of function. Initialization from incompatible pointer type
- Defining a vector of struct and passing it as pointer in C - error: initialization from incompatible pointer type
- How do I fix a segmentation fault with a pointer being passed from a function in C?
- C how can i resolve assignment from incompatible pointer type when using struct* = struct
- How can I fix incompatible pointer to integer conversion assigning to 'int' from 'void *'
- how to fix incompatible pointer types passing 'char ' to parameter of type 'FILE *' (aka 'struct __sFILE *') [-Wincompatible-pointer-types]
- avr-gcc warning: initialization from incompatible pointer type
- warning: initialization from incompatible pointer type
- initialization from incompatible pointer type - C structs
- Initialization from incompatible pointer type when constructing a GTK combobox
- Pointer Functions in C: initialization of foo from incompatible pointer type
- C,GCC warning: initialization from incompatible pointer type
- Warning: passing argument 2 of 'transform_labels' from incompatible pointer type [-Wincompatible-pointer-types]|
- How to point at incompatible pointer type in C?
- error: passing argument 1 of ‘kthread_create_on_node’ from incompatible pointer type
More Query from same tag
- cuda file did not link with function defined in C file
- C - Float Imprecision, How to lock the decimal places for calculations?
- NSUInteger C Array
- Using fread properly
- STM32L011 cannot set USART in rx mode
- hiredis getting err 1 REDIS_ERR_IO No such file or directory
- Get List of Time Zones Across Platforms in C
- How can array name and the address of the array name both prints the same value?
- Constant Pointers vs Pointers to Constant Strings
- Aligning printf() variables and decimals in C
- Why is it not possible to replace stdout with pipe output directly?
- why buffer overflow happens?
- Code that prints only five-digit numbers with spaces
- gcc undefined reference when I know the references are correct
- Activate and deactivate usb device in c?
- Move node to a new index in a linked list
- IPMI: How to find the threshold values using ipmimonitoring-sensors.c
- My consumer thread isn't reading my producer thread's products correctly
- Calculate how many hours since the epoch?
- Don't understand simple C Program if/else result
- How much memory would be freed if pointer is changed in C?
- Ordered Binary Tree insert
- Simplifying a Recursive Function
- Changing an inputted integer into a defined constant value
- Setting userdata as namespace in Lua
- How to get number of NIC RX rings in Linux
- can't compile c code
- Inlining typedef for a function returning a function
- Error: Variable-sized object may not be initialized
- How to get the expected answer
Copyright 2023 www.appsloveworld.com . All rights reserved.
- Create Account
Home Posts Topics Members FAQ
Join Bytes to post your question to a community of 472,950 software developers and data experts.
Sign in to post your reply or Sign up for a free account.
To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.