HatchJS Logo

HatchJS.com

Cracking the Shell of Mystery

Incompatible Pointer Types in C: A Guide to Fixing Errors

Avatar

Incompatible Pointer Types in C

Pointers are a powerful tool in C, but they can also be a source of errors. One common error is using an incompatible pointer type. This can happen when you try to dereference a pointer of one type with a variable of another type. For example, you might try to dereference a pointer to a char with a variable of type int. This will cause a compiler error.

In this article, we’ll take a closer look at incompatible pointer types in C. We’ll discuss what they are, why they occur, and how to avoid them. We’ll also provide some examples of incompatible pointer types in action.

By the end of this article, you’ll have a good understanding of incompatible pointer types in C and how to avoid them.

Incompatible Pointer Type Reason Solution
Dereferencing a null pointer Attempting to access the value of a pointer that has not been initialized to a valid address will result in a segmentation fault. Ensure that all pointers are initialized to a valid address before dereferencing them.
Assigning a pointer to an incompatible type Attempting to assign a pointer to a type that is not compatible with the pointer’s original type will result in a compile-time error. Ensure that the pointer’s original type and the type to which you are assigning it are compatible.
Passing a pointer to a function with an incompatible type Attempting to pass a pointer to a function with an incompatible type will result in a compile-time error. Ensure that the pointer’s original type and the function’s parameter type are compatible.

What is an incompatible pointer type?

In C, a pointer is a variable that stores the address of another variable. When you declare a pointer, you must specify the type of the variable that it points to. For example, the following declaration creates a pointer to an integer:

The pointer `p` can be used to access the value of the integer variable that it points to. For example, the following code prints the value of the integer variable that `p` points to:

c printf(“The value of the integer variable is %d\n”, *p);

However, if you try to assign a pointer of one type to a variable of another type, you will get a compiler error. For example, the following code will generate a compiler error:

c int *p; char *q;

p = q; // Error: incompatible pointer types

This is because the pointer `p` is of type `int *`, and the pointer `q` is of type `char *`. These two types are incompatible, and you cannot assign a pointer of one type to a variable of another type.

An incompatible pointer type is a pointer that is not of the same type as the variable that it points to. This can happen for a variety of reasons, such as:

* **Casting a pointer to an incompatible type.** For example, the following code casts a pointer to an integer to a pointer to a character:

c int *p = 0; char *q = (char *)p; // Error: incompatible pointer types

* **Using a pointer to an array of one type to access an element of an array of another type.** For example, the following code uses a pointer to an array of integers to access an element of an array of characters:

c int arr1[] = {1, 2, 3}; char arr2[] = {‘a’, ‘b’, ‘c’};

int *p = arr1; char c = p[0]; // Error: incompatible pointer types

* **Using a pointer to a structure of one type to access a member of a structure of another type.** For example, the following code uses a pointer to a structure of type `struct point` to access a member of a structure of type `struct circle`:

c struct point { int x; int y; };

struct circle { struct point center; int radius; };

struct point *p = &circle.center; int x = p->x; // Error: incompatible pointer types

What are the causes of incompatible pointer types?

There are a number of reasons why you might get an incompatible pointer type error. Some of the most common causes include:

  • Casting a pointer to an incompatible type. This is the most common cause of incompatible pointer type errors. When you cast a pointer to an incompatible type, you are essentially telling the compiler that you know what you are doing and that you are sure that the pointer is safe to use. However, in most cases, you are not aware of the potential dangers of casting a pointer to an incompatible type, and you are likely to make a mistake.
  • Using a pointer to an array of one type to access an element of an array of another type. This is another common cause of incompatible pointer type errors. When you use a pointer to an array of one type to access an element of an array of another type, you are essentially telling the compiler that you know that the arrays are the same size and that you are sure that the pointer is safe to use. However, in most cases, you are not aware of the potential dangers of using a pointer to an array of one type to access an element of an array of another type, and you are likely to make a mistake.
  • Using a pointer to a structure of one type to access a member of a structure of another type. This is another common cause of incompatible pointer type errors. When you use a pointer to a structure of one type to access a member of a structure of another type, you are essentially telling the compiler that you know that the structures are the same size and that you are sure that the pointer is safe to use. However, in most cases, you are not aware of the potential dangers of using a pointer to a structure of one type to access a member of a structure of another type, and you are likely to make a mistake.

Incompatible pointer types can

How to fix incompatible pointer types?

Incompatible pointer types occur when you try to assign a value of one pointer type to a variable of another pointer type. This can happen when you are trying to pass a pointer to a function that expects a different pointer type, or when you are trying to cast a pointer to a different type.

There are a few ways to fix incompatible pointer types.

  • Use a type cast. A type cast is a way to explicitly convert a value of one type to another type. To use a type cast, you use the `(type)` syntax, where `type` is the type you want to convert the value to. For example, if you have a pointer to a `char` and you want to cast it to a pointer to a `int`, you would use the following syntax:

c int *intPtr = (int *)charPtr;

  • Use a function pointer. A function pointer is a pointer to a function. You can use a function pointer to call a function with a different pointer type than the function’s declared type. To use a function pointer, you declare a variable of type `function_pointer`, and then assign the address of the function to the variable. For example, the following code declares a function pointer to a function that takes a `char` pointer and returns an `int`:

c typedef int (*MyFunction)(char *);

MyFunction myFunctionPtr = myFunction;

You can then call the function using the function pointer, like this:

c int result = myFunctionPtr(“Hello world!”);

  • Use a struct. A struct is a collection of data elements that are grouped together. You can use a struct to store data of different types in a single variable. To use a struct, you declare a struct variable, and then add the data elements to the struct. For example, the following code declares a struct called `MyStruct` that has two data elements: a `char` pointer and an `int`:

c struct MyStruct { char *str; int num; };

struct MyStruct myStruct = {“Hello world!”, 10};

You can then access the data elements of the struct using the dot operator. For example, the following code prints the value of the `str` data element of the `myStruct` variable:

c printf(“str: %s\n”, myStruct.str);

Examples of incompatible pointer types

Here are some examples of incompatible pointer types:

  • A pointer to a `char` cannot be assigned to a pointer to a `int`.
  • A pointer to a function that takes a `char` pointer cannot be called with a pointer to an `int` pointer.
  • A struct that has a `char` pointer cannot be assigned to a struct that has an `int` pointer.

In each of these cases, the compiler will generate an error when you try to compile the code.

Incompatible pointer types can be a source of errors in C programs. By understanding how to fix incompatible pointer types, you can avoid these errors and write more robust code.

Q: What is an incompatible pointer type error?

A: An incompatible pointer type error occurs when you try to use a pointer of one type with a variable of another type. For example, you might try to use a pointer to a char with a variable of type int. This error can occur for a number of reasons, but the most common is when you are trying to pass a pointer to a function that expects a different type of pointer.

Q: How can I fix an incompatible pointer type error?

A: There are a few ways to fix an incompatible pointer type error. The first is to make sure that the pointer and the variable are of the same type. If they are not, you can either cast the pointer to the correct type or change the type of the variable.

Another way to fix an incompatible pointer type error is to use a different function. If the function you are trying to call expects a different type of pointer, you can find a different function that accepts the type of pointer you have.

Finally, you can also try to use a pointer to a pointer. This means that you create a pointer to a variable of one type, and then you use that pointer to access a variable of another type. This can be a bit more complicated, but it can sometimes be the only way to fix an incompatible pointer type error.

Q: What are some common causes of incompatible pointer type errors?

A: There are a few common causes of incompatible pointer type errors. The first is when you are trying to pass a pointer to a function that expects a different type of pointer. For example, you might try to pass a pointer to a char to a function that expects a pointer to an int. This error can also occur when you are trying to access a member of a structure or class using a pointer of the wrong type.

Another common cause of incompatible pointer type errors is when you are trying to dereference a pointer that is not initialized. When you dereference a pointer, you are essentially accessing the memory that the pointer points to. If the pointer is not initialized, then you will not be able to access any memory, and you will get an incompatible pointer type error.

Finally, incompatible pointer type errors can also occur when you are trying to cast a pointer to a different type. When you cast a pointer, you are essentially changing the type of the pointer. This can be done to make the pointer compatible with a function or variable that expects a different type of pointer. However, if you cast a pointer to the wrong type, you will get an incompatible pointer type error.

Q: How can I prevent incompatible pointer type errors?

A: There are a few things you can do to prevent incompatible pointer type errors. The first is to make sure that you are using the correct types for your pointers and variables. If you are not sure what type to use, you can always check the documentation for the function or variable you are using.

Another way to prevent incompatible pointer type errors is to use a type-safe language. Type-safe languages prevent you from making mistakes with types, so you can be confident that your code will not have any incompatible pointer type errors.

Finally, you can also use a compiler that has a warning for incompatible pointer type errors. This warning will let you know if you are trying to use a pointer of one type with a variable of another type. This can help you to catch incompatible pointer type errors before they cause problems in your code.

Incompatible pointer types in C can be a common source of errors. This is because pointers are used to reference memory locations, and if the types of the pointers are not compatible, then the compiler will not be able to correctly track the memory locations that the pointers are referencing. This can lead to unexpected behavior and errors.

There are a few ways to avoid incompatible pointer types in C. One way is to use the correct type for the pointer. For example, if you are trying to reference a variable of type int, then you should use a pointer of type int*. Another way to avoid incompatible pointer types is to use the type cast operator. The type cast operator can be used to convert a pointer of one type to a pointer of another type. However, it is important to use the type cast operator correctly, as it can also lead to errors.

If you are unsure about whether or not two pointer types are compatible, then you can always check the compiler documentation. The compiler documentation will provide a list of the different types of pointers that are supported, and it will also provide information on how to use the type cast operator correctly.

By following these tips, you can help to avoid incompatible pointer types in C and ensure that your code is correct and runs without errors.

Author Profile

Marcus Greenwood

Latest entries

  • December 26, 2023 Error Fixing User: Anonymous is not authorized to perform: execute-api:invoke on resource: How to fix this error
  • December 26, 2023 How To Guides Valid Intents Must Be Provided for the Client: Why It’s Important and How to Do It
  • December 26, 2023 Error Fixing How to Fix the The Root Filesystem Requires a Manual fsck Error
  • December 26, 2023 Troubleshooting How to Fix the `sed unterminated s` Command

Similar Posts

Tensorflow attributeerror: module ‘tensorflow’ has no attribute ‘session’.

AttributeError: module ‘tensorflow’ has no attribute ‘session’ TensorFlow is a powerful open-source library for machine learning. It’s used by researchers and developers around the world to build and train neural networks. However, TensorFlow can sometimes be a bit tricky to use, and one common error that people encounter is the AttributeError: module ‘tensorflow’ has no…

ValueError: Optimizer got an empty parameter list

Have you ever tried to use an optimizer in PyTorch, only to get an error message like “ValueError: optimizer got an empty parameter list”? If so, you’re not alone. This is a common error that can occur when you’re not using the optimizer correctly. In this article, we’ll take a look at what causes this…

ModuleNotFoundError: No module named urllib2

ModuleNotFoundError: No module named urllib2 Have you ever tried to import the `urllib2` module in Python and received the error `ModuleNotFoundError: No module named urllib2`? If so, you’re not alone. This is a common error that can occur for a variety of reasons. In this article, we’ll take a look at what causes this error…

dataframe.groupby Attribute Error: How to Fix It

DataFrame.groupby() Attribute Error: ‘DataFrame’ object has no attribute ‘groupby’ This error message is a common one for Python users who are trying to use the `groupby()` method on a `DataFrame` object. The `groupby()` method is used to split a `DataFrame` into groups based on a specified column or set of columns. However, if you try…

How to Fix the ipython is not defined Error

IPython is not defined Have you ever been working on a Python project and gotten the error message “ipython is not defined”? This can be a frustrating experience, especially if you’re not sure what’s causing the problem. In this article, we’ll take a look at what iPython is, why it might not be defined, and…

OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to

OpenSSL SSL_connect: SSL_ERROR_SYSCALL in Connection to When you’re trying to connect to a secure website, the last thing you want to see is an error message. But that’s exactly what happens when you get the OpenSSL SSL_connect: SSL_ERROR_SYSCALL error. This error can be caused by a number of things, but it usually means that there’s…

Next: Type Conversions , Previous: Functions , Up: Top   [ Contents ][ Index ]

23 Compatible Types

Declaring a function or variable twice is valid in C only if the two declarations specify compatible types. In addition, some operations on pointers require operands to have compatible target types.

In C, two different primitive types are never compatible. Likewise for the defined types struct , union and enum : two separately defined types are incompatible unless they are defined exactly the same way.

However, there are a few cases where different types can be compatible:

  • Every enumeration type is compatible with some integer type. In GNU C, the choice of integer type depends on the largest enumeration value.
  • Array types are compatible if the element types are compatible and the sizes (when specified) match.
  • Pointer types are compatible if the pointer target types are compatible.
  • Function types that specify argument types are compatible if the return types are compatible and the argument types are compatible, argument by argument. In addition, they must all agree in whether they use ... to allow additional arguments.
  • Function types that don’t specify argument types are compatible if the return types are.
  • Function types that specify the argument types are compatible with function types that omit them, if the return types are compatible and the specified argument types are unaltered by the argument promotions (see Argument Promotions ).

In order for types to be compatible, they must agree in their type qualifiers. Thus, const int and int are incompatible. It follows that const int * and int * are incompatible too (they are pointers to types that are not compatible).

If two types are compatible ignoring the qualifiers, we call them nearly compatible . (If they are array types, we ignore qualifiers on the element types. 7 ) Comparison of pointers is valid if the pointers’ target types are nearly compatible. Likewise, the two branches of a conditional expression may be pointers to nearly compatible target types.

If two types are compatible ignoring the qualifiers, and the first type has all the qualifiers of the second type, we say the first is upward compatible with the second. Assignment of pointers requires the assigned pointer’s target type to be upward compatible with the right operand (the new value)’s target type.

This is a GNU C extension.

Have a Question?

If you have any question you can ask below or enter what you are looking for!

  • Programming /

“assignment from incompatible pointer type [enabled by default]”?

I’m fairly new to C and trying to write a function that inserts a node at the beginning of the list, using these two structs:

My function is

When I compile I get the warning “assignment from incompatible pointer type [enabled by default]” for the last 3 lines so obviously there’s something I’m missing.

Avatar for davy.ai

November 24, 2023 at 12:05 am

The warning “assignment from incompatible pointer type” occurs because there is a mismatch in pointer types in the last 3 lines of the function.

In the sin_list struct, the “first” and “last” members are declared as pointers to structs of type “node”. However, in the function, they are being assigned with “newnode” which is declared as a pointer to struct “singly_linked_list_node” (i.e., node). This causes the pointer type mismatch.

To fix this issue, you need to remove the struct keyword from the assignment statements in the function:

You should also ensure that you have included the necessary header files at the beginning of your code:

Additionally, there is an error when allocating memory for the “data” member of the newnode. Instead of sizeof(length) , it should be length to allocate the correct amount of memory for the string. You can modify the line to:

These changes should resolve the warning and ensure proper functionality of your insert_at_start function.

Comments are closed.

Ubuntu Forums

  • Unanswered Posts
  • View Forum Leaders
  • Contact an Admin
  • Forum Council
  • Forum Governance
  • Forum Staff

Ubuntu Forums Code of Conduct

  • Forum IRC Channel
  • Get Kubuntu
  • Get Xubuntu
  • Get Lubuntu
  • Get Ubuntu Studio
  • Get Ubuntu Cinnamon
  • Get Edubuntu
  • Get Ubuntu Unity
  • Get Ubuntu Kylin
  • Get Ubuntu Budgie
  • Get Ubuntu Mate
  • Ubuntu Code of Conduct
  • Ubuntu Wiki
  • Community Wiki
  • Launchpad Answers
  • Ubuntu IRC Support
  • Official Documentation
  • User Documentation
  • Distrowatch
  • Bugs: Ubuntu
  • PPAs: Ubuntu
  • Web Upd8: Ubuntu
  • OMG! Ubuntu
  • Ubuntu Insights
  • Planet Ubuntu
  • Full Circle Magazine
  • Activity Page
  • Please read before SSO login
  • Advanced Search

Home

  • The Ubuntu Forum Community
  • Ubuntu Specialised Support
  • Development & Programming
  • Programming Talk
  • [SOLVED] gcc complains: assignment from incompatible pointer type

Thread: gcc complains: assignment from incompatible pointer type

Thread tools.

  • Show Printable Version
  • Subscribe to this Thread…
  • Linear Mode
  • Switch to Hybrid Mode
  • Switch to Threaded Mode
  • View Profile
  • View Forum Posts
  • Private Message

Chiel92 is offline

gcc complains: assignment from incompatible pointer type

Hi all, I am a c noob and I'm experiencing problems with my program. I have the following code: Code: typedef struct { char ***state; struct Node *parent; } Node; Node* NodeInit(Node *node, char ***state, Node *parent) { (*node).state = state; (*node).parent = parent; return node; } But it gives the following error: Code: 40:17: warning: assignment from incompatible pointer type [enabled by default] where line 40 is the line which states: "(*node).parent = parent;" What am I doing wrong? Any help is appreciated. Regards
Vim - Comfortable text editing and coding vim-autoformat - a code-formatting plugin for vim supporting various languages - https://github.com/Chiel92/vim-autoformat Vimperator - Fast browsing using keyboard only

dwhitney67 is offline

Re: gcc complains: assignment from incompatible pointer type

Originally Posted by Chiel92 What am I doing wrong? The problem lies with how you have your struct defined; try something like this: Code: typedef struct Node <-- Add Node label here { char ***state; struct Node *parent; } Node; I wish I had a good answer as to why the compiler doesn't complain at the structure, but instead complains at the code (as you found out). Btw, out of curiosity, what are your plans for 'state'. Is it going to be an two-dim array of strings?

trent.josephsen is offline

Better yet, eliminate the typedef altogether. typedef should be used to make code clearer, not to avoid typing 'struct' when the code in question needs to know that node is, in fact, a struct. I regret that propriety and the forum rules do not permit me to use more forceful terms in admonishing this practice, which I revile with a burning passion. No offense to OP, because I'm sure you innocently copied it from someone else (who should know better), but it's something I see frequently and it irks me every single time.
Thanks for your quick replies! dwhitney67: Your suggestion solves the problem, though I do not understand exactly why it works. trent.josephsen: I didn't know how to get recusive structs in C, so I indeed googled somewhat and copied the suggestions. They happened to use typedef, so I concluded that typedef was necessary in order to obtain a correct recursive struct. I now have the following code which also works fine. I do not really mind typing the 'struct' statement every time I declare a Node, so following the suggestion of trent.josephsen I removed it and it works Code: struct Node { char **state; struct Node *parent; }; struct Node* NodeInit(struct Node *node, char **state, struct Node *parent) { (*node).state = state; (*node).parent = parent; return node; } dwhitney67: state is a pointer to a 2d array of chars. But I realized that the pointer to the first element of the 2d array can also be used for passing by reference, so I removed one of the asterisks. Many thanks for your help!
Thanks for seeing the light Hiding "struct" with a typedef is a common practice, and I don't have a problem with it when it's used reasonably -- but it hides information that can be important to readability. I use the guideline that if you are just passing it around as a black box and don't care what's in it, it's fair game for a typedef (like FILE, which is an example from the Standard); but if you plan to access its members in any way you should declare it with 'struct'. Same goes for pointers, if you're going to dereference it you should be able to tell it's a pointer by looking at its declaration.

11jmb is offline

Originally Posted by trent.josephsen I use the guideline that if you are just passing it around as a black box and don't care what's in it, it's fair game for a typedef That's an excellent way to put it. A more general guideline would be that a typedef is supposed to provide more information, never to remove useful information. Just to expand a bit, you used the example of FILE, which is passed along like a black box, and therefore the "struct" identifier is not terribly useful. An example of where the struct identifier should be kept might be tm or timespec in time.h, since the members will almost definitely be accessed. Therefore, in this case using typedef to hide the struct identifier would make the code less readable.
  • Private Messages
  • Subscriptions
  • Who's Online
  • Search Forums
  • Forums Home
  • New to Ubuntu
  • General Help
  • Installation & Upgrades
  • Desktop Environments
  • Networking & Wireless
  • Multimedia Software
  • Ubuntu Development Version
  • Virtualisation
  • Server Platforms
  • Ubuntu Cloud and Juju
  • Packaging and Compiling Programs
  • Development CD/DVD Image Testing
  • Ubuntu Application Development
  • Ubuntu Dev Link Forum
  • Bug Reports / Support
  • System76 Support
  • Apple Hardware Users
  • Recurring Discussions
  • Mobile Technology Discussions (CLOSED)
  • Announcements & News
  • Weekly Newsletter
  • Membership Applications
  • The Fridge Discussions
  • Forum Council Agenda
  • Request a LoCo forum
  • Resolution Centre
  • Ubuntu/Debian BASED
  • Arch and derivatives
  • Fedora/RedHat and derivatives
  • Mandriva/Mageia
  • Slackware and derivatives
  • openSUSE and SUSE Linux Enterprise
  • Gentoo and derivatives
  • Any Other OS
  • Assistive Technology & Accessibility
  • Art & Design
  • Education & Science
  • Documentation and Community Wiki Discussions
  • Outdated Tutorials & Tips
  • Ubuntu Women
  • Arizona Team - US
  • Arkansas Team - US
  • Brazil Team
  • California Team - US
  • Canada Team
  • Centroamerica Team
  • Instalación y Actualización
  • Colombia Team - Colombia
  • Georgia Team - US
  • Illinois Team
  • Indiana - US
  • Kentucky Team - US
  • Maine Team - US
  • Minnesota Team - US
  • Mississippi Team - US
  • Nebraska Team - US
  • New Mexico Team - US
  • New York - US
  • North Carolina Team - US
  • Ohio Team - US
  • Oklahoma Team - US
  • Oregon Team - US
  • Pennsylvania Team - US
  • Texas Team - US
  • Uruguay Team
  • Utah Team - US
  • Virginia Team - US
  • West Virginia Team - US
  • Australia Team
  • Bangladesh Team
  • Hong Kong Team
  • Myanmar Team
  • Philippine Team
  • Singapore Team
  • Albania Team
  • Catalan Team
  • Portugal Team
  • Georgia Team
  • Ireland Team - Ireland
  • Kenyan Team - Kenya
  • Kurdish Team - Kurdistan
  • Lebanon Team
  • Morocco Team
  • Saudi Arabia Team
  • Tunisia Team
  • Other Forums & Teams
  • Afghanistan Team
  • Alabama Team - US
  • Alaska Team - US
  • Algerian Team
  • Andhra Pradesh Team - India
  • Austria Team
  • Bangalore Team
  • Bolivia Team
  • Cameroon Team
  • Colorado Team - US
  • Connecticut Team
  • Costa Rica Team
  • Ecuador Team
  • El Salvador Team
  • Florida Team - US
  • Galician LoCo Team
  • Hawaii Team - US
  • Honduras Team
  • Idaho Team - US
  • Iowa Team - US
  • Jordan Team
  • Kansas Team - US
  • Louisiana Team - US
  • Maryland Team - US
  • Massachusetts Team
  • Michigan Team - US
  • Missouri Team - US
  • Montana Team - US
  • Namibia Team
  • Nevada Team - US
  • New Hampshire Team - US
  • New Jersey Team - US
  • Northeastern Team - US
  • Panama Team
  • Paraguay Team
  • Quebec Team
  • Rhode Island Team - US
  • Senegal Team
  • South Carolina Team - US
  • South Dakota Team - US
  • Switzerland Team
  • Tamil Team - India
  • Tennessee Team - US
  • Trinidad & Tobago Team
  • Uganda Team
  • United Kingdom Team
  • US LoCo Teams
  • Venezuela Team
  • Washington DC Team - US
  • Washington State Team - US
  • Wisconsin Team
  • Za Team - South Africa
  • Zimbabwe Team

Tags for this Thread

View Tag Cloud

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is Off
  • HTML code is Off
  • Ubuntu Forums

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

I keep getting an error: incompatible pointer types passing 'string' (aka 'char *') to parameter of type 'string *' (aka 'char **')

user11913821's user avatar

This may sound mean, but the shift function is a mess. First, there's the error of the parameter mismatch. Note that declaring a string is really declaring a char * . Then, the parameter is itself an array. By declaring it with int shift(string argv[]) you've declared a pointer to a char pointer, or char ** . Next, you've used argv as the parameter name. This is a really bad practice. You think you're using the argv[] array, but you're actually declaring a shadow array - an array that has the same name, but is totally unrelated. You should use a unique parameter name. Never use any of the "reserved" or default variable names like argc or argv in a declaration. Further, if you want to pass a string as a parameter, declare it as a string var, not an array. For example, you could use int shift(string mystring)

This would help you resolve that particular error, but there are bigger issues that will likely make this a moot point.

In reading your code, it's not clear what you were trying to do. Part of it looks like it is trying to cycle through the key string to 'shift' all of it's elements. Other parts look like it was trying to change only one char and return that value to the calling function. Then, it falls apart by trying to 'shift' j, which is the index value used to control the for loop. J has nothing to do with the key or any character in the key. BTW, the for loop will only execute once because of the return statements. The program will never cycle back to the top of the for loop. In short, the shift function as written can't make up its mind what it wants to do! A rewrite is in order.

Do you want to convert a single char or all the chars in a string? Will it only be used on the key or also on the plaintext?

And then, in the main program, how are you going to switch from one char to the next in the key?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

Cliff B's user avatar

You must log in to answer this question.

Not the answer you're looking for browse other questions tagged pset2 vigenere ..

  • Featured on Meta
  • Upcoming initiatives on Stack Overflow and across the Stack Exchange network...
  • We spent a sprint addressing your requests — here’s how it went

Hot Network Questions

  • Staying in USA longer than 3 months
  • Is there a drawback to using Heart's blood rote repeatedly?
  • Why are my star jasmine flowers turning brown
  • Can someone explain the Trump immunity ruling?
  • Unsorted Intersection
  • Turning Misty step into a reaction to dodge spells/attacks
  • How far back in time have historians estimated the rate of economic growth and the economic power of various empires?
  • What's the point of Dream Chaser?
  • Geometry question about a six-pack of beer
  • Why does the Trump immunity decision further delay the trial?
  • Sitting on a desk or at a desk? What's the diffrence?
  • Correlation for Small Dataset?
  • Is anything implied by the author using the (Greek) phrase translated "work with your hands" in 1 Thessalonians 4:11?
  • How to clean up interrupted edge loops using geometry nodes and fill holes with quad faces?
  • spath3 rotations shrink paths
  • lme4 Inconsistency
  • Imagining Graham's number in your head collapses your head to a Black hole
  • Exercise 5.B.7(b) in "Linear Algebra Done Right" 4th edition by Sheldon Axler
  • Why is pressure in the outermost layer of a star lower than at its center?
  • Why does Paul's fight with Feyd-Rautha take so long?
  • Is it unfair to retroactively excuse a student for absences?
  • Who originated the idea that the purpose of government is to protect its citizens?
  • Travel to Mexico from India, do I need to pay a fee?
  • Did Tolkien give his son explicit permission to publish all that unfinished material?

assignment of incompatible pointer type

Home Posts Topics Members FAQ

New Member |Select|Wrap|Line Numbers When I try to do the following:-
|Select|Wrap|Line Numbers I get the above warning. 10403 Recognized Expert Moderator Expert and are both pointers but the types are not compatible for assignment. So you probably need to ask and answer the question what are the types of those 2 expressions.
Message
 

Sign in to post your reply or Sign up for a free account.

Similar topics

| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use .

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.

Brand Logo

Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode .

Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

  • Special Interest Groups

Fixing `-Wincompatible-pointer-types` compiler warning

aha_1980

I'm doing polymorphism in C (without ++, it's an embedded target where no C++ compiler is available).

All is working well so far, but when I compile the code with a recent GCC (for the unit tests), I have dozens of incompatible-pointer-types warnings which I'd like to fix (if possible).

The minimal example is as follows:

So in principle the compiler tells me, that I cannot mix void * with Foo * here. Any ideas how to fix this warning?

Thanks and regards

Qt has to stay free or it will die.

JonB

@aha_1980 said in Fixing &#x60;-Wincompatible-pointer-types&#x60; compiler warning :

I'm doing polymorphism in C

That's a new one on me! But never mind, I get the gist.

So you can see why the complaint. Your table demands functions which take a void * parameter but you are trying to set from a function which takes a Foo * .

To fix you must cast the actual function pointers to match the table's formal function definition. Create a convenient typedef TABLEFUNC for the int (*function)(void *) signature and

yes that would work (it is typedef int (*TABLEFUNC)(void *) as definition, but otherwise alright.

The only problem with that: the real table contains about 20 different TABLEFUNC definition, i.e. TABLEFUNC0 , TABLEFUNC1 , ... TABLEFUNC19 ). So a bit of work, but still manageable.

Except someone has an easier hint ;)

@aha_1980 what do you mean with I have dozens of imcompatible-pointer-types warnings which I'd like to fix (if possible)

Don't tell me you're not running with -Werror 😱

I would like to help, but I have no idea, sorry :(

Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

Q: What's that? A: It's blue light. Q: What does it do? A: It turns blue.

@J-Hilk the embedded compiler does not even complain about that.

And for gcc, I have to fix the ~100 warnings first before enabling such options ;)

kshegunov

You have to cast the pointers explicitly (as @JonB said). Ideally I'd do this with macro-magic, where I declare a table for a class with a macro, I start/end the table definition for a specific structure with a macro and define each of the entries with another. Also I'd do this at init time (just saying).

I have a question, though. Q: Why is the method taking void * , why not take directly an object pointer?

@JonB said in Fixing &#x60;-Wincompatible-pointer-types&#x60; compiler warning :

That's a new one on me!
C++ didn't invent this idea. It's as old as programming, more or less.

Read and abide by the Qt Code of Conduct

@kshegunov said in Fixing &#x60;-Wincompatible-pointer-types&#x60; compiler warning :

If you look around, you'll see as many references saying you cannot do polymorphism from C as those which say you can. with cheating-function-pointers :)
The only problem with that: the real table contains about 20 different TABLEFUNC definition, i.e. TABLEFUNC0, TABLEFUNC1, ... TABLEFUNC19). So a bit of work, but still manageable.

I don't understand. The typedef /macro is a single one for the type of the table elements , not the type(s) of the functions (which could vary) you put in it? So if you have one "the real table" I don't see why you want 20 typedefs? EDIT From subsequent posts, here I realise I misunderstood. I thought you had an array of same-typed-function-pointers to which you wanted to assign various different actual functions. Looking carefully that does not correspond at all to the example code you gave, but it did apply to me when I had to do this in C during the last millennium... :)

Since presumably you have just one table initialisation in one place (or maybe it's 20, not sure which), you could #pragma that warning off around the initialisations? Unless you regard that as worse, and do want code which actually passes the warning....
Q: Why is the method taking void *, why not take directly an object pointer?

Because next to the fooTable , there is a barTable and a bazTable , each having its own type.

I don't understand. The typedef/macro is a single one for the type of the table elements, not the type(s) of the functions (which could vary) you put in it? So if you have one "the real table" I don't see why you want 20 typedefs?
The real table looks like this:

So you need to have typedefs for each function prototype. Some might be reuseable, but probably it's better to have one for each row.

Since presumably you have just one table initialisation in one

No, I have a handful implementations of that.

@kshegunov idea of having a macro to create all the tables sounds great, I'll need to try that.

Thanks for your help

Nothing cheating about them. This is what the C++ compiler does under the hood, and it's been known from the "invention" of virtual . It's a concept, it isn't some black magic, and the concept predates the language implementation. Try to use a virtual method in a class constructor and see how well it works before having a fully resolved vptr if you don't believe me.

I don't understand. The typedef /macro is a single one for the type of the table elements , not the type(s) of the functions (which could vary) you put in it? So if you have one "the real table" I don't see why you want 20 typedefs?

Functions may take a different set of arguments, I imagine.

I personally would.

Because next to the fooTable, there is a barTable and a bazTable, each having its own type.

Yeah, I think that's "more correct" approach. Consider:

expanding to something like:

@kshegunov I'll try that tomorrow. Sounds like a clever, reuseable, and clean macro solution :)

and clean macro solution

As much as such a thing exists ;P

@kshegunov It doesn't - But in C it's the only possibility ;)

Yes, sorry, I edited my earlier post, I quite misunderstood and thought you had an array of function pointers to which you wanted to assign.

But now there is something odd in your case. Since you have

that implies you are writing out exactly the required signature for each function pointer in the struct . In which case, why don't they match correctly against the functions you are assigning to them, then you wouldn't need casts...? That is why I was thinking of the array-of-function-pointers situation, where you do have a problem with one array element type and mutiple different function types to assign.

As @kshegunov already wrote, the first pointer, the void * is specialized for each implementation struct - think of inheritance.

So the "base class" has void * and the implementations have Foo * resp. Bar * .

Nothing wrong with that, just that the compiler warns at this point (which is a bit pointless imho, as ever pointer is compatible to void * , but ok.

Nothing wrong with that, just that the compiler warns at this point (which is a bit pointless imho, as ever pointer is compatible to void *, but ok.

Yes, every pointer decays implicitly to void * , but that's not what the compiler whines about. It complains because the function prototypes are different, hence the actual functions may be different, the compiler can't tell out of the box.

What I don't get is: if these classes do not share some base class ( Foo , Bar , or something else), it's a bit hard to think what you're doing in C++ to either of them as a parameter to a function when all they have in common is they are pointers to something unknown?

You don't have to answer/justify yourself. I realise you doubtless know what you are doing and have your own reasons. But that's what strikes me.

What I don't get is: if these classes do not share some base class (Foo, Bar, or something else), it's a bit hard to think what you're doing in C++ to either of them as a parameter to a function when all they have in common is they are pointers to something unknown?
What is a base class in C?

Oh damn! I forgot already this is not C++, sorry... !

OK, well, I still wonder what the shared function does being handed pointers to different C struct s, when you don't know what struct it is...?

Well, as far as I understood the task the point is to allow inheritance support for a language (and a compiler) which doesn't provide it. This entails (if you follow what C++ does) having a static table of methods for each "class". Each "inherited" table then is supposedly referencing the base class' table and further allowing it to be extended. And if you want at the end you can get dynamic polymorphism in. All in all it's not a trivial thing to do, but should be doable with some magic.

assignment of incompatible pointer type

  • First post Last post Go to my next post
  • Get Qt Extensions
  • Search Search

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.

How to get rid of "warning: initialization from incompatible pointer type [-Wincompatible-pointer-types"]

After building I get these warnings: /Users/janhkila/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-spi.c: In function ‘spiTransferBytesNL’: /Users/janhkila/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-spi.c:922:39: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types] uint8_t * last_out8 = &result[c_longs-1]; ^ /Users/janhkila/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-spi.c:923:40: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types] uint8_t * last_data8 = &last_data;

What can I do to silence this warning?

  • will be automatically fixed once a new stable Arduino-ESP32 version releases, per HAL/SPI initializes from incompatible pointer type. · Issue #5039 · espressif/arduino-esp32 · GitHub , new code is this
  • if you want to silence the warning locally, you can directly fix it at the source ( /Users/janhkila/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-spi.c ), this fixes it globally for all projects
  • You can try and pass the -Wno-incompatible-pointer-types ( docs ) in the build_flags for a per-project fix.

I tried replacing the contents of this file with the one you linked to as the new one, but the differences are too big (some includes not found and other errors)

If you’re still having issues with this I think I found the relevant changes, I think the line numbers changed since more changes were added to the file. You want these changes: Currently found here

uint8_t * last_out8 = (uint8_t *)&result[c_longs-1]; uint8_t * last_data8 = (uint8_t *)&last_data;
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
  • OverflowAI GenAI features for Teams
  • OverflowAPI Train & fine-tune LLMs
  • Labs The future of collective knowledge sharing
  • About the company Visit the blog

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Get early access and see previews of new features.

assignment from incompatible pointer type

I have set up the following struct:

... and then I have defined:

where thread_arr is thread_node_t *thread_arr = NULL;

I don't understand why the compiler is complaining. Maybe I'm misunderstanding something.

N 1.1's user avatar

2 Answers 2

Shouldn't struct thread_node_t *next; be struct _thread_node_t *next;

Also, do away with the explicit cast.

  • OMG! Yeah... thanks. I can't believe I missed that. I had a struct like this working in a previous program and I was killing myself because I couldn't figure it out. Thanks a bunch. –  Hristo Commented Apr 21, 2010 at 14:52
  • About the explicit cast: No need for ANSI C, but required for C++. –  Macmade Commented Apr 21, 2010 at 15:00
  • Or for that matter, thread_node_t *thread_node = malloc(sizeof *thread_node); . –  Steve Jessop Commented Apr 21, 2010 at 15:00
  • 1 @Macmade: I'm not wholly decided whether to cast the result of malloc or not, but one of the things that swings it is that as you say, if you leave out the cast it won't compile as C++. I consider this a good reason to leave out the cast! –  Steve Jessop Commented Apr 21, 2010 at 15:01

It's because thread_arr is a thread_node_t pointer, and your next member is a struct thread_node_t pointer. Not the same thing.

Macmade's user avatar

  • What I'm trying to work out, is what is a struct thread_node_t ? Why does the questioner's typedef compile? –  Steve Jessop Commented Apr 21, 2010 at 14:55
  • 1 Oh, I get it, it's a forward declaration of an incomplete type. –  Steve Jessop Commented Apr 21, 2010 at 14:58

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Sign up or log in

Post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .

Not the answer you're looking for? Browse other questions tagged c pointers struct typedef or ask your own question .

  • The Overflow Blog
  • How to build open source apps in a highly regulated industry
  • Community Products Roadmap Update, July 2024
  • Featured on Meta
  • We spent a sprint addressing your requests — here’s how it went
  • Upcoming initiatives on Stack Overflow and across the Stack Exchange network...
  • Policy: Generative AI (e.g., ChatGPT) is banned
  • The [lib] tag is being burninated
  • What makes a homepage useful for logged-in users

Hot Network Questions

  • Boundary Conditions on the Inlet and Outlet in a Discontinuous Galerkin framework
  • Is arxiv strictly for new stuff?
  • Line from Song KÄMPFERHERZ
  • Examples of distribution for which first-order condition is not enough for MLE
  • What is the value of air anisotropy?
  • mirrorlist.centos.org no longer resolve?
  • Exercise 5.B.7(b) in "Linear Algebra Done Right" 4th edition by Sheldon Axler
  • Can a criminal litigator introduce new evidence if it is pursuant to the veracity of a winess?
  • Is "conversational" used correctly in this context?
  • How far back in time have historians estimated the rate of economic growth and the economic power of various empires?
  • Can you arrange 25 whole numbers (not necessarily all different) so that the sum of any three successive terms is even but the sum of all 25 is odd?
  • Why should the Vce be half the value of the supply source?
  • Why should I meet my advisor even if I have nothing to report?
  • Do Christians believe that Jews and Muslims go to hell?
  • Are inductors in resonant circuits dangerous? They produce very high voltages
  • Turning Misty step into a reaction to dodge spells/attacks
  • Geometry question about a six-pack of beer
  • Does the decision of North Korea sending engineering troops to the occupied territory in Ukraine leave them open to further UN sanctions?
  • What’s the highest salary the greedy king can arrange for himself?
  • Travel to Mexico from India, do I need to pay a fee?
  • If a lambda is declared as a default argument, is it different for each call site?
  • Why is Uranus colder than Neptune?
  • Did the BBC censor a non-binary character in Transformers: EarthSpark?
  • Ideal diode in parallel with resistor and voltage source

assignment of incompatible pointer type

Incompatible Pointer Type Warning: Function Return

Resolving Incompatible Pointer Type Warnings in C: Function Return Type

Abstract: Learn how to resolve incompatible pointer type warnings when returning a function pointer in C.

Resolving Incompatible Pointer Type Warnings: Function Return Type

Function pointers are a powerful feature in C that allows functions to be passed as arguments to other functions, or returned as values from functions. However, using function pointers can sometimes result in incompatible pointer type warnings. This article will focus on resolving these warnings when the issue is related to the function return type.

Understanding the Problem

When a function is declared to return a function pointer, the return type must match the type of the function pointer being returned. For example, consider the following code:

The above code will generate a warning because the return type of my\_function is void , but the type of func\_ptr is void(*)(void\*) . The missing void\* parameter in the return type of my\_function is causing the incompatible pointer type warning.

Solving the Problem

To resolve the incompatible pointer type warning, the return type of the function being assigned to the function pointer must match the type of the function pointer. In the previous example, this can be achieved by modifying the return type of my\_function as follows:

By adding the void\* parameter to the return type of my\_function , the return type now matches the type of func\_ptr , and the warning is resolved.

Using Function Pointers with Different Parameter Types

It is also possible to use function pointers with different parameter types. For example, consider the following code:

The above code will generate a warning because the type of func\_ptr is void(*)(int) , but the return type of my\_function is void(*)(void\*) . To resolve this warning, a cast can be used to convert the return type of my\_function to the type of func\_ptr .

By casting the return type of my\_function to the type of func\_ptr , the warning is resolved.

Incompatible pointer type warnings related to function return types can be resolved by ensuring that the return type of the function being assigned to the function pointer matches the type of the function pointer. If the parameter types of the function and the function pointer do not match, a cast can be used to convert the return type of the function to the type of the function pointer. By understanding these concepts, developers can use function pointers effectively in their C code.

  • C Function Pointers Tutorial, https://www.codingunit.com/c-tutorial-function-pointers
  • Function Pointer Casts in C, https://stackoverflow.com/questions/8258817/function-pointer-casts-in-c
  • Function Pointer Tutorial, https://www.codeproject.com/Articles/7054/Function-Pointer-Tutorial-with-C-Cplusplus

Tags: :  C Programming Pointer Warning

Latest news

  • Login Not Working: Troubleshooting Submission Issues in Angular
  • Debugging an Elixir App Crashing in a Kubernetes Environment
  • Error with Provisioning Profile 'XXXXXX' in Github Actions for Mac Devices
  • PyTorch: Gradient Computation Fails for First Slice of Input Tensor
  • Getting Started with SimpleLoggingAdvisor in Spring Boot using Spring AI
  • Creating a Searchbox with Dropdown Filter Options using JavaScript, HTML, and Flask
  • Effectively Wrapping Double Physical Units in C#
  • Troubleshooting 'Learn Next.js': Dynamic Router Returns 404 Error during Invoice Page Editing
  • Automating Order Syncing Between Two WooCommerce Sites: A Clone Approach
  • Finding Properties Ending with '.0' in Neo4j
  • Grouping Factors in Excel Tables for Sector Reports using openxlsx in R
  • Making Form Fields Read-Only in Odoo 16 Based on Field Value
  • Optimizing MTU for Low Latency Networks in ISP Environment
  • Converting Single-Line Heading in Tiptap React Editor
  • Specifying Output in Jupyter Notebook: Direct Output in Cells
  • Properly Filling a GridPane with Variable Amount of Buttons in JavaFX
  • Calculating Probability Densities with Gamma-Distributed Response Variable in R: GLMER Model
  • Resolving TypeError in AWS Glue ETL Job: Missing Argument in DynamicFrameWriter from JDBC Conf
  • Java: Test Failure - Build Passing on Mac, Failing on Windows
  • SpringBoot 3.x App: Defining @Bean void return type from 3rd Party Library using @Configuration class
  • Using Full Width Rows Instead of Assigned Col widths in Col Divs
  • Merging Specific Cells in DataGridView: A Two-Form Solution
  • Error in Django Web Application: Seconds of Loading Time and Code Displaying
  • Customizing Color Themes in Visual Studio 2022 C# Editor: A Step-by-Step Guide
  • Handling Emit Events with Vue.extend in TypeScript
  • Using a RADIUS Server with Python on Windows
  • Error Running Grantrole with Email Address
  • Migrating Legacy Google Auth Credential Manager: Check if Old App Already Uses New Library?
  • DelphiXE3 Application Loses Oracle 19c (AWS RDS) Connection After VPN Change (Cisco GlobalProtect)
  • Stopping Specific Animations in Unity: A New Input System Case Study
  • AngularJS UI-Select Filtering One Field: A Solution
  • Tracking IP Addresses on a Raspberry Pi Zero: Essential for Remote Access
  • Migrating React Native App using Expo Amplify: AccessDenied error uploading file in S3
  • Debugging STM32 GPIORegisters with STM32CubeIDE: Errors and Solutions
  • Android Fragment Crash After Pressing Back Button Immediately

assignment of incompatible pointer type

Get notified in your email when a new post is published to this blog

Writing a remove_all_pointers type trait, part 2

' data-src=

Raymond Chen

June 28th, 2024 2 0

Last time, we wrote a remove_ all_ pointers type trait , but I noted that even though we found a solution, we weren’t finished yet.

We can bring back the one-liner by using a different trick to delay the recursion: Don’t ask for the type until we know we really are recursing.

The sketch is

We first define a type_holder to be a type which has a type member type that holds our answer. If T is a pointer, then the type holder is the recursive call. Otherwise, the type holder is a dummy type whose sole purpose is to have a type member type that produces T again.

We can now pack up that if into a std:: conditional .

It turns out that we don’t need to define a dummy : The C++ standard library comes with one built in! It’s called std:: type_ identity<T> , available starting in C++20. ( We looked at std:: type_ identity<T> a little while ago .)

Now we can inline the type_holder .

Or even better, just derive from the type_holder !

' data-src=

Leave a comment Cancel reply

Log in to join the discussion or edit/delete existing comments.

The Aha moment after comparing the initial attempt and the final approach: A manual design of short circuitry.

So basically moving the ::type outside the std::conditional_t fixes it, because this way you don’t construct the template unless it’s necessary. (But the last optimisation is nice.)

  • remove_all_pointers type trait, part 2" title="Share on Twitter" aria-label="Share on Twitter" target="_blank" rel="noopener noreferrer nofollow" class="sublink twitter" tabindex="-1">

light-theme-icon

Insert/edit link

Enter the destination URL

Or link to existing content

IMAGES

  1. assignment from incompatible pointer type-CSDN博客

    assignment of incompatible pointer type

  2. Array : Warning about assignment from incompatible pointer type when using pointers and arrays?

    assignment of incompatible pointer type

  3. Incompatible pointer types assigning

    assignment of incompatible pointer type

  4. (1)malloc:assignment from incompatible pointer type -(2)declaration of

    assignment of incompatible pointer type

  5. C언어 Struct 에서 warning : assignment from incompatible pointer type

    assignment of incompatible pointer type

  6. [Solved] C warning: incompatible pointer types passing

    assignment of incompatible pointer type

VIDEO

  1. Tugas Struktur Data Menggunakan Pointer pada Pascal

  2. "Debugging Python: How to Fix 'TypeError: unsupported operand types for + 'NoneType' and 'str'"

  3. What are Pointers? How to Use Them? and How they can Improve your C++ Programming Skills

  4. SOLVED error: member access within null pointer of type 'TreeNode'

  5. C++ Pointers: Common Problems and How to Solve Them|| Dangling, Wild , Null Pointers|| Lecture 13

  6. Pointers, Void Pointers, Why use void pointers in C++ (English/Urdu)

COMMENTS

  1. c

    5. Here: ptr_one = &x; ptr_two = &x; ptr_three = &y; ptr_one, ptr_two and ptr_three are int* s and &x and &y are double* s. You are trying to assign an int* with a double*. Hence the warning. Fix it by changing the types of the pointers to double* instead of int*, i.e, change the following lines.

  2. c

    1. There is no such type in C as int[][], only the first part of a multidimensional array can be unspecified. So int[][5] is okay. In addition to the other answers posted here, if you can use C99, you can use variable arrays to accomplish what you want: void box_sort(int N, int M, int x[M][N]);

  3. Incompatible Pointer Types in C: A Guide to Fixing Errors

    char *q; p = q; // Error: incompatible pointer types. This is because the pointer `p` is of type `int *`, and the pointer `q` is of type `char *`. These two types are incompatible, and you cannot assign a pointer of one type to a variable of another type. An incompatible pointer type is a pointer that is not of the same type as the variable ...

  4. Compatible Types (GNU C Language Manual)

    Likewise for the defined types struct, union and enum: two separately defined types are incompatible unless they are defined exactly the same way. However, there are a few cases where different types can be compatible: Every enumeration type is compatible with some integer type. In GNU C, the choice of integer type depends on the largest ...

  5. "assignment from incompatible pointer type [enabled by default]"?

    The warning "assignment from incompatible pointer type" occurs because there is a mismatch in pointer types in the last 3 lines of the function. In the sin_list struct, the "first" and "last" members are declared as pointers to structs of type "node". However, in the function, they are being assigned with "newnode" which is ...

  6. c

    incompatible pointer types passing 'string' (aka 'char *') to parameter of type 'string *' (aka 'char **') Ask Question Asked 3 years, 11 months ago. Modified 2 months ago. Viewed 2k times 0 Im writing a funtion that takes string as a argument and gives back a integer. This function counts lenght of a string entered at the command line argument.

  7. Incompatible pointer types even though they are both char*?

    One, the variable that refers to the strcmp function must be one of the type char *, which previously in the course is known as a string, the one you are passing is of type char **, although you do not write it that way but as char * [46]. This type of variables, as you define it, declares an array of pointers (46 in particular), it is pointers ...

  8. gcc complains: assignment from incompatible pointer type

    Re: gcc complains: assignment from incompatible pointer type. Better yet, eliminate the typedef altogether. typedef should be used to make code clearer, not to avoid typing 'struct' when the code in question needs to know that node is, in fact, a struct. I regret that propriety and the forum rules do not permit me to use more forceful terms in ...

  9. pset2

    Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site

  10. c

    So that they (pointers) would be compatible they shall be pointers to compatible types. 6.7.6.1 Pointer declarators. 2 For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types. However types int or char on the one hand and type void on the other hand are not compatible types.

  11. Warning: assignment from incompatible pointer type

    In function `QuestMenu_BuildListMenuTemplate': src/quests.c:758: warning: assignment from incompatible pointer type. Where line 758 is: gMultiuseListMenuTemplate.itemPrintFunc = QuestMenu_ItemPrintFunc; If you need more information I'd be happy to update the post! Thank you for your time :)

  12. warning: assignment from incompatible pointer type

    1 10240. Banfa. 9,065 Recognized ExpertModeratorExpert. assignment from incompatible pointer type means that you are assigning a value to a pointer variable from a pointer of a different and incompatible type. In your code that means that p->firstnode and &node are both pointers but the types are not compatible for assignment.

  13. Fixing `-Wincompatible-pointer-types` compiler warning

    Fixing `-Wincompatible-pointer-types` compiler warning. I'm doing polymorphism in C (without ++, it's an embedded target where no C++ compiler is available). All is working well so far, but when I compile the code with a recent GCC (for the unit tests), I have dozens of incompatible-pointer-types warnings which I'd like to fix (if possible).

  14. Double void pointer gives "incompatible pointer types"

    It also does implicit conversion of r-value void* to any pointer type, also of equal or stricter CV-qualifier, although it can cause compile warning, and is prohibited in C++. C does not allow implicit conversion of a pointer to void* to a pointer to a pointer of other type, and vice versa. As soon as the result is not void* value, or argument ...

  15. Resolving Incompatible Pointer Types

    Resolving Incompatible Pointer Types. In ISO C, a pointer to void can be assigned to a pointer of any other type. You do not need to cast the pointer explicitly. C++ allows void pointers to be assigned only to other void pointers. If you use C memory functions that return void pointers (such as malloc(), calloc(), realloc() ), each void pointer ...

  16. [SOLVED]: Do not understand: assignment from incompatible pointer type

    Another example is the "& " operator: &sTest is a pointer to a fixed-sized array and has type "const char [5]* "; Both sTest and &sTest point to the same place, but have different types. Thus assigning sTest works because both pointers are of the same type. Assigning &sTest also works, but since the types are different, it generates a warning.

  17. How to get rid of "warning: initialization from incompatible pointer

    You can try and pass the -Wno-incompatible-pointer-types in the build_flags for a per-project fix. wewowo2125 May 6, 2021, 7:16am 4. I tried replacing the contents of this file with the one you linked to as the new one, but the differences are too big (some includes not found and other errors) supleed2 May 22, 2021 ...

  18. c

    Assignment from incompatible pointer type with structs. 0. Can't assign address of struct to a variable whose type is a typedef of pointer to that struct. 0. Incompatible pointer types assigning to struct. 0. Pointer assignment causing EXC_BAD_ACCESS. 1.

  19. Resolving Incompatible Pointer Type Warnings in C: Function Return Type

    To resolve the incompatible pointer type warning, the return type of the function being assigned to the function pointer must match the type of the function pointer. In the previous example, this can be achieved by modifying the return type of my\_function as follows: void (*func\_ptr)(void\*) = NULL; void my\_function(void\*) {.

  20. Writing a remove_all_pointers type trait, part 2

    We first define a type_holder to be a type which has a type member type that holds our answer. If T is a pointer, then the type holder is the recursive call. Otherwise, the type holder is a dummy type whose sole purpose is to have a type member type that produces T again. We can now pack up that if into a std:: conditional.