Add a certain number of bytes to a void pointer in C










1















I have been banging my ahead on this problem for many hours now. Suppose you have a function that takes an int value as parameter. What I need to do is take this int value and interpret whatever the value is as # of bytes to be added to an address:



void* foo(int size)
node->pointer += size; //assume node->pointer is of type void*



What the above does is actually add size*4 bytes to pointer because int is 4 bytes. casting this as (char*) i.e.



node->pointer += (char*)size; 


this does't work.
How do I do this in the most simple way?










share|improve this question



















  • 1





    Try node->pointer = (void*)((char*)(node->pointer) + size);

    – squeamish ossifrage
    Nov 14 '18 at 1:45











  • Thank you so much! I can't believe I overlooked casting it back to void!

    – whoshotya
    Nov 14 '18 at 1:49











  • @squeamishossifrage you don't need to explicitly cast to void* because char* is automatically converted to void*. Right?

    – Ajay Brahmakshatriya
    Nov 14 '18 at 2:11












  • Your title asks how to add a number of bytes to a “void pointer,” but your question implies you are adding to a pointer to int, where it says “What the above does is actually add size*4 bytes to pointer because int is 4 bytes.” What is the actual type of node->pointer?

    – Eric Postpischil
    Nov 14 '18 at 11:54











  • @EricPostpischil It's void* type

    – whoshotya
    Nov 19 '18 at 2:19















1















I have been banging my ahead on this problem for many hours now. Suppose you have a function that takes an int value as parameter. What I need to do is take this int value and interpret whatever the value is as # of bytes to be added to an address:



void* foo(int size)
node->pointer += size; //assume node->pointer is of type void*



What the above does is actually add size*4 bytes to pointer because int is 4 bytes. casting this as (char*) i.e.



node->pointer += (char*)size; 


this does't work.
How do I do this in the most simple way?










share|improve this question



















  • 1





    Try node->pointer = (void*)((char*)(node->pointer) + size);

    – squeamish ossifrage
    Nov 14 '18 at 1:45











  • Thank you so much! I can't believe I overlooked casting it back to void!

    – whoshotya
    Nov 14 '18 at 1:49











  • @squeamishossifrage you don't need to explicitly cast to void* because char* is automatically converted to void*. Right?

    – Ajay Brahmakshatriya
    Nov 14 '18 at 2:11












  • Your title asks how to add a number of bytes to a “void pointer,” but your question implies you are adding to a pointer to int, where it says “What the above does is actually add size*4 bytes to pointer because int is 4 bytes.” What is the actual type of node->pointer?

    – Eric Postpischil
    Nov 14 '18 at 11:54











  • @EricPostpischil It's void* type

    – whoshotya
    Nov 19 '18 at 2:19













1












1








1








I have been banging my ahead on this problem for many hours now. Suppose you have a function that takes an int value as parameter. What I need to do is take this int value and interpret whatever the value is as # of bytes to be added to an address:



void* foo(int size)
node->pointer += size; //assume node->pointer is of type void*



What the above does is actually add size*4 bytes to pointer because int is 4 bytes. casting this as (char*) i.e.



node->pointer += (char*)size; 


this does't work.
How do I do this in the most simple way?










share|improve this question
















I have been banging my ahead on this problem for many hours now. Suppose you have a function that takes an int value as parameter. What I need to do is take this int value and interpret whatever the value is as # of bytes to be added to an address:



void* foo(int size)
node->pointer += size; //assume node->pointer is of type void*



What the above does is actually add size*4 bytes to pointer because int is 4 bytes. casting this as (char*) i.e.



node->pointer += (char*)size; 


this does't work.
How do I do this in the most simple way?







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 2:12









Shiladitya

9,41892030




9,41892030










asked Nov 14 '18 at 1:42









whoshotyawhoshotya

193




193







  • 1





    Try node->pointer = (void*)((char*)(node->pointer) + size);

    – squeamish ossifrage
    Nov 14 '18 at 1:45











  • Thank you so much! I can't believe I overlooked casting it back to void!

    – whoshotya
    Nov 14 '18 at 1:49











  • @squeamishossifrage you don't need to explicitly cast to void* because char* is automatically converted to void*. Right?

    – Ajay Brahmakshatriya
    Nov 14 '18 at 2:11












  • Your title asks how to add a number of bytes to a “void pointer,” but your question implies you are adding to a pointer to int, where it says “What the above does is actually add size*4 bytes to pointer because int is 4 bytes.” What is the actual type of node->pointer?

    – Eric Postpischil
    Nov 14 '18 at 11:54











  • @EricPostpischil It's void* type

    – whoshotya
    Nov 19 '18 at 2:19












  • 1





    Try node->pointer = (void*)((char*)(node->pointer) + size);

    – squeamish ossifrage
    Nov 14 '18 at 1:45











  • Thank you so much! I can't believe I overlooked casting it back to void!

    – whoshotya
    Nov 14 '18 at 1:49











  • @squeamishossifrage you don't need to explicitly cast to void* because char* is automatically converted to void*. Right?

    – Ajay Brahmakshatriya
    Nov 14 '18 at 2:11












  • Your title asks how to add a number of bytes to a “void pointer,” but your question implies you are adding to a pointer to int, where it says “What the above does is actually add size*4 bytes to pointer because int is 4 bytes.” What is the actual type of node->pointer?

    – Eric Postpischil
    Nov 14 '18 at 11:54











  • @EricPostpischil It's void* type

    – whoshotya
    Nov 19 '18 at 2:19







1




1





Try node->pointer = (void*)((char*)(node->pointer) + size);

– squeamish ossifrage
Nov 14 '18 at 1:45





Try node->pointer = (void*)((char*)(node->pointer) + size);

– squeamish ossifrage
Nov 14 '18 at 1:45













Thank you so much! I can't believe I overlooked casting it back to void!

– whoshotya
Nov 14 '18 at 1:49





Thank you so much! I can't believe I overlooked casting it back to void!

– whoshotya
Nov 14 '18 at 1:49













@squeamishossifrage you don't need to explicitly cast to void* because char* is automatically converted to void*. Right?

– Ajay Brahmakshatriya
Nov 14 '18 at 2:11






@squeamishossifrage you don't need to explicitly cast to void* because char* is automatically converted to void*. Right?

– Ajay Brahmakshatriya
Nov 14 '18 at 2:11














Your title asks how to add a number of bytes to a “void pointer,” but your question implies you are adding to a pointer to int, where it says “What the above does is actually add size*4 bytes to pointer because int is 4 bytes.” What is the actual type of node->pointer?

– Eric Postpischil
Nov 14 '18 at 11:54





Your title asks how to add a number of bytes to a “void pointer,” but your question implies you are adding to a pointer to int, where it says “What the above does is actually add size*4 bytes to pointer because int is 4 bytes.” What is the actual type of node->pointer?

– Eric Postpischil
Nov 14 '18 at 11:54













@EricPostpischil It's void* type

– whoshotya
Nov 19 '18 at 2:19





@EricPostpischil It's void* type

– whoshotya
Nov 19 '18 at 2:19












2 Answers
2






active

oldest

votes


















2














If you don't mind using GCC extensions (Pointer arithmetic on void pointers and function pointers) and you're actually using GCC (or Clang), then you can write (more or less) what you wrote using +=:



void foo(int size)

node->pointer += size; // Not standard — using GCC extension



It is not standard C, though, and therefore not portable. From C11 (ISO/IEC 9899:2011):





  • §6.2.5 Types ¶19: The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.


  • §6.5.3.4 The sizeof and _Alignof operators: The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or …


  • §6.5.6 Additive operators: For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type.


  • §6.5.6 Additive operators: For subtraction, one of the following shall hold:

    • both operands have arithmetic type;

    • both operands are pointers to qualified or unqualified versions of compatible complete object types; or

    • the left operand is a pointer to a complete object type and the right operand has integer type.



  • §6.5.16.2 Compound assignment ¶1: For the operators += and -= only, either the left operand shall be an atomic, qualified, or unqualified pointer to a complete object type, and the right shall have integer type; or the left operand shall have atomic, qualified, or unqualified arithmetic type, and the right shall have arithmetic type.



Since the void type is incomplete and cannot be completed, and therefore a void * is a pointer to an incomplete type, you can't use void with sizeof, nor can you use void * with + or - to do pointer arithmetic, and consequently you can't use void * with the compound assignments += and -= either.



The standard C equivalent would be:



void foo(int size)

node->pointer = (char *)node->pointer + size;



This code doesn't return a value, any more than yours did (though you said it would because the return type was void *). Since node->pointer is a void *, you don't need a cast back to void *; this is C and the conversion is automatic.






share|improve this answer

























  • This is fine in Visual Studio as well. Do you mean it's incompatible in some odd platforms?

    – Barmak Shemirani
    Nov 14 '18 at 2:15






  • 1





    I mean that the C standard says it is not allowed. I've not yet got my quotes lined up. One is C11 §6.2.5 Types ¶19; there will need to be others. But you can't determine the size of an incomplete type, and void can't be completed, and to increment a pointer, you need to know the size in bytes, so…but as I said, I've not got all the relevant quotes in place just yet, and I need to take an hour or two out before I can expand my answer — there's some "life" that needs to be lived.

    – Jonathan Leffler
    Nov 14 '18 at 2:19











  • Sorry, I misunderstood your answer about what's standard and what's not standard, even though it's written clearly. node->pointer = (char *)node->pointer + size; works in Visual Studio, not the other.

    – Barmak Shemirani
    Nov 14 '18 at 2:48












  • I've added the most relevant sections of the C standard.

    – Jonathan Leffler
    Nov 14 '18 at 5:43











  • Since there is a C standard way to do this, would’t it be better for an answer to lead with that? The GCC extension seems to add little or no value.

    – Eric Postpischil
    Nov 14 '18 at 11:51


















0














writing a sample code for your reference. I used similar concept as answered above.



#include <stdio.h>

struct node
int payload;
void *ptr;
*n;

void foo(int x)
n->ptr = (char *)n->ptr +x;


int main()
n = malloc(sizeof(struct node));
n->payload = 10; //random payload
n->ptr = n;

printf("%pn", n->ptr);
foo(2); //increment by 2
printf("%pn", n->ptr);






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291982%2fadd-a-certain-number-of-bytes-to-a-void-pointer-in-c%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    If you don't mind using GCC extensions (Pointer arithmetic on void pointers and function pointers) and you're actually using GCC (or Clang), then you can write (more or less) what you wrote using +=:



    void foo(int size)

    node->pointer += size; // Not standard — using GCC extension



    It is not standard C, though, and therefore not portable. From C11 (ISO/IEC 9899:2011):





    • §6.2.5 Types ¶19: The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.


    • §6.5.3.4 The sizeof and _Alignof operators: The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or …


    • §6.5.6 Additive operators: For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type.


    • §6.5.6 Additive operators: For subtraction, one of the following shall hold:

      • both operands have arithmetic type;

      • both operands are pointers to qualified or unqualified versions of compatible complete object types; or

      • the left operand is a pointer to a complete object type and the right operand has integer type.



    • §6.5.16.2 Compound assignment ¶1: For the operators += and -= only, either the left operand shall be an atomic, qualified, or unqualified pointer to a complete object type, and the right shall have integer type; or the left operand shall have atomic, qualified, or unqualified arithmetic type, and the right shall have arithmetic type.



    Since the void type is incomplete and cannot be completed, and therefore a void * is a pointer to an incomplete type, you can't use void with sizeof, nor can you use void * with + or - to do pointer arithmetic, and consequently you can't use void * with the compound assignments += and -= either.



    The standard C equivalent would be:



    void foo(int size)

    node->pointer = (char *)node->pointer + size;



    This code doesn't return a value, any more than yours did (though you said it would because the return type was void *). Since node->pointer is a void *, you don't need a cast back to void *; this is C and the conversion is automatic.






    share|improve this answer

























    • This is fine in Visual Studio as well. Do you mean it's incompatible in some odd platforms?

      – Barmak Shemirani
      Nov 14 '18 at 2:15






    • 1





      I mean that the C standard says it is not allowed. I've not yet got my quotes lined up. One is C11 §6.2.5 Types ¶19; there will need to be others. But you can't determine the size of an incomplete type, and void can't be completed, and to increment a pointer, you need to know the size in bytes, so…but as I said, I've not got all the relevant quotes in place just yet, and I need to take an hour or two out before I can expand my answer — there's some "life" that needs to be lived.

      – Jonathan Leffler
      Nov 14 '18 at 2:19











    • Sorry, I misunderstood your answer about what's standard and what's not standard, even though it's written clearly. node->pointer = (char *)node->pointer + size; works in Visual Studio, not the other.

      – Barmak Shemirani
      Nov 14 '18 at 2:48












    • I've added the most relevant sections of the C standard.

      – Jonathan Leffler
      Nov 14 '18 at 5:43











    • Since there is a C standard way to do this, would’t it be better for an answer to lead with that? The GCC extension seems to add little or no value.

      – Eric Postpischil
      Nov 14 '18 at 11:51















    2














    If you don't mind using GCC extensions (Pointer arithmetic on void pointers and function pointers) and you're actually using GCC (or Clang), then you can write (more or less) what you wrote using +=:



    void foo(int size)

    node->pointer += size; // Not standard — using GCC extension



    It is not standard C, though, and therefore not portable. From C11 (ISO/IEC 9899:2011):





    • §6.2.5 Types ¶19: The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.


    • §6.5.3.4 The sizeof and _Alignof operators: The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or …


    • §6.5.6 Additive operators: For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type.


    • §6.5.6 Additive operators: For subtraction, one of the following shall hold:

      • both operands have arithmetic type;

      • both operands are pointers to qualified or unqualified versions of compatible complete object types; or

      • the left operand is a pointer to a complete object type and the right operand has integer type.



    • §6.5.16.2 Compound assignment ¶1: For the operators += and -= only, either the left operand shall be an atomic, qualified, or unqualified pointer to a complete object type, and the right shall have integer type; or the left operand shall have atomic, qualified, or unqualified arithmetic type, and the right shall have arithmetic type.



    Since the void type is incomplete and cannot be completed, and therefore a void * is a pointer to an incomplete type, you can't use void with sizeof, nor can you use void * with + or - to do pointer arithmetic, and consequently you can't use void * with the compound assignments += and -= either.



    The standard C equivalent would be:



    void foo(int size)

    node->pointer = (char *)node->pointer + size;



    This code doesn't return a value, any more than yours did (though you said it would because the return type was void *). Since node->pointer is a void *, you don't need a cast back to void *; this is C and the conversion is automatic.






    share|improve this answer

























    • This is fine in Visual Studio as well. Do you mean it's incompatible in some odd platforms?

      – Barmak Shemirani
      Nov 14 '18 at 2:15






    • 1





      I mean that the C standard says it is not allowed. I've not yet got my quotes lined up. One is C11 §6.2.5 Types ¶19; there will need to be others. But you can't determine the size of an incomplete type, and void can't be completed, and to increment a pointer, you need to know the size in bytes, so…but as I said, I've not got all the relevant quotes in place just yet, and I need to take an hour or two out before I can expand my answer — there's some "life" that needs to be lived.

      – Jonathan Leffler
      Nov 14 '18 at 2:19











    • Sorry, I misunderstood your answer about what's standard and what's not standard, even though it's written clearly. node->pointer = (char *)node->pointer + size; works in Visual Studio, not the other.

      – Barmak Shemirani
      Nov 14 '18 at 2:48












    • I've added the most relevant sections of the C standard.

      – Jonathan Leffler
      Nov 14 '18 at 5:43











    • Since there is a C standard way to do this, would’t it be better for an answer to lead with that? The GCC extension seems to add little or no value.

      – Eric Postpischil
      Nov 14 '18 at 11:51













    2












    2








    2







    If you don't mind using GCC extensions (Pointer arithmetic on void pointers and function pointers) and you're actually using GCC (or Clang), then you can write (more or less) what you wrote using +=:



    void foo(int size)

    node->pointer += size; // Not standard — using GCC extension



    It is not standard C, though, and therefore not portable. From C11 (ISO/IEC 9899:2011):





    • §6.2.5 Types ¶19: The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.


    • §6.5.3.4 The sizeof and _Alignof operators: The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or …


    • §6.5.6 Additive operators: For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type.


    • §6.5.6 Additive operators: For subtraction, one of the following shall hold:

      • both operands have arithmetic type;

      • both operands are pointers to qualified or unqualified versions of compatible complete object types; or

      • the left operand is a pointer to a complete object type and the right operand has integer type.



    • §6.5.16.2 Compound assignment ¶1: For the operators += and -= only, either the left operand shall be an atomic, qualified, or unqualified pointer to a complete object type, and the right shall have integer type; or the left operand shall have atomic, qualified, or unqualified arithmetic type, and the right shall have arithmetic type.



    Since the void type is incomplete and cannot be completed, and therefore a void * is a pointer to an incomplete type, you can't use void with sizeof, nor can you use void * with + or - to do pointer arithmetic, and consequently you can't use void * with the compound assignments += and -= either.



    The standard C equivalent would be:



    void foo(int size)

    node->pointer = (char *)node->pointer + size;



    This code doesn't return a value, any more than yours did (though you said it would because the return type was void *). Since node->pointer is a void *, you don't need a cast back to void *; this is C and the conversion is automatic.






    share|improve this answer















    If you don't mind using GCC extensions (Pointer arithmetic on void pointers and function pointers) and you're actually using GCC (or Clang), then you can write (more or less) what you wrote using +=:



    void foo(int size)

    node->pointer += size; // Not standard — using GCC extension



    It is not standard C, though, and therefore not portable. From C11 (ISO/IEC 9899:2011):





    • §6.2.5 Types ¶19: The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.


    • §6.5.3.4 The sizeof and _Alignof operators: The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or …


    • §6.5.6 Additive operators: For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type.


    • §6.5.6 Additive operators: For subtraction, one of the following shall hold:

      • both operands have arithmetic type;

      • both operands are pointers to qualified or unqualified versions of compatible complete object types; or

      • the left operand is a pointer to a complete object type and the right operand has integer type.



    • §6.5.16.2 Compound assignment ¶1: For the operators += and -= only, either the left operand shall be an atomic, qualified, or unqualified pointer to a complete object type, and the right shall have integer type; or the left operand shall have atomic, qualified, or unqualified arithmetic type, and the right shall have arithmetic type.



    Since the void type is incomplete and cannot be completed, and therefore a void * is a pointer to an incomplete type, you can't use void with sizeof, nor can you use void * with + or - to do pointer arithmetic, and consequently you can't use void * with the compound assignments += and -= either.



    The standard C equivalent would be:



    void foo(int size)

    node->pointer = (char *)node->pointer + size;



    This code doesn't return a value, any more than yours did (though you said it would because the return type was void *). Since node->pointer is a void *, you don't need a cast back to void *; this is C and the conversion is automatic.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 5:38

























    answered Nov 14 '18 at 1:48









    Jonathan LefflerJonathan Leffler

    564k906751023




    564k906751023












    • This is fine in Visual Studio as well. Do you mean it's incompatible in some odd platforms?

      – Barmak Shemirani
      Nov 14 '18 at 2:15






    • 1





      I mean that the C standard says it is not allowed. I've not yet got my quotes lined up. One is C11 §6.2.5 Types ¶19; there will need to be others. But you can't determine the size of an incomplete type, and void can't be completed, and to increment a pointer, you need to know the size in bytes, so…but as I said, I've not got all the relevant quotes in place just yet, and I need to take an hour or two out before I can expand my answer — there's some "life" that needs to be lived.

      – Jonathan Leffler
      Nov 14 '18 at 2:19











    • Sorry, I misunderstood your answer about what's standard and what's not standard, even though it's written clearly. node->pointer = (char *)node->pointer + size; works in Visual Studio, not the other.

      – Barmak Shemirani
      Nov 14 '18 at 2:48












    • I've added the most relevant sections of the C standard.

      – Jonathan Leffler
      Nov 14 '18 at 5:43











    • Since there is a C standard way to do this, would’t it be better for an answer to lead with that? The GCC extension seems to add little or no value.

      – Eric Postpischil
      Nov 14 '18 at 11:51

















    • This is fine in Visual Studio as well. Do you mean it's incompatible in some odd platforms?

      – Barmak Shemirani
      Nov 14 '18 at 2:15






    • 1





      I mean that the C standard says it is not allowed. I've not yet got my quotes lined up. One is C11 §6.2.5 Types ¶19; there will need to be others. But you can't determine the size of an incomplete type, and void can't be completed, and to increment a pointer, you need to know the size in bytes, so…but as I said, I've not got all the relevant quotes in place just yet, and I need to take an hour or two out before I can expand my answer — there's some "life" that needs to be lived.

      – Jonathan Leffler
      Nov 14 '18 at 2:19











    • Sorry, I misunderstood your answer about what's standard and what's not standard, even though it's written clearly. node->pointer = (char *)node->pointer + size; works in Visual Studio, not the other.

      – Barmak Shemirani
      Nov 14 '18 at 2:48












    • I've added the most relevant sections of the C standard.

      – Jonathan Leffler
      Nov 14 '18 at 5:43











    • Since there is a C standard way to do this, would’t it be better for an answer to lead with that? The GCC extension seems to add little or no value.

      – Eric Postpischil
      Nov 14 '18 at 11:51
















    This is fine in Visual Studio as well. Do you mean it's incompatible in some odd platforms?

    – Barmak Shemirani
    Nov 14 '18 at 2:15





    This is fine in Visual Studio as well. Do you mean it's incompatible in some odd platforms?

    – Barmak Shemirani
    Nov 14 '18 at 2:15




    1




    1





    I mean that the C standard says it is not allowed. I've not yet got my quotes lined up. One is C11 §6.2.5 Types ¶19; there will need to be others. But you can't determine the size of an incomplete type, and void can't be completed, and to increment a pointer, you need to know the size in bytes, so…but as I said, I've not got all the relevant quotes in place just yet, and I need to take an hour or two out before I can expand my answer — there's some "life" that needs to be lived.

    – Jonathan Leffler
    Nov 14 '18 at 2:19





    I mean that the C standard says it is not allowed. I've not yet got my quotes lined up. One is C11 §6.2.5 Types ¶19; there will need to be others. But you can't determine the size of an incomplete type, and void can't be completed, and to increment a pointer, you need to know the size in bytes, so…but as I said, I've not got all the relevant quotes in place just yet, and I need to take an hour or two out before I can expand my answer — there's some "life" that needs to be lived.

    – Jonathan Leffler
    Nov 14 '18 at 2:19













    Sorry, I misunderstood your answer about what's standard and what's not standard, even though it's written clearly. node->pointer = (char *)node->pointer + size; works in Visual Studio, not the other.

    – Barmak Shemirani
    Nov 14 '18 at 2:48






    Sorry, I misunderstood your answer about what's standard and what's not standard, even though it's written clearly. node->pointer = (char *)node->pointer + size; works in Visual Studio, not the other.

    – Barmak Shemirani
    Nov 14 '18 at 2:48














    I've added the most relevant sections of the C standard.

    – Jonathan Leffler
    Nov 14 '18 at 5:43





    I've added the most relevant sections of the C standard.

    – Jonathan Leffler
    Nov 14 '18 at 5:43













    Since there is a C standard way to do this, would’t it be better for an answer to lead with that? The GCC extension seems to add little or no value.

    – Eric Postpischil
    Nov 14 '18 at 11:51





    Since there is a C standard way to do this, would’t it be better for an answer to lead with that? The GCC extension seems to add little or no value.

    – Eric Postpischil
    Nov 14 '18 at 11:51













    0














    writing a sample code for your reference. I used similar concept as answered above.



    #include <stdio.h>

    struct node
    int payload;
    void *ptr;
    *n;

    void foo(int x)
    n->ptr = (char *)n->ptr +x;


    int main()
    n = malloc(sizeof(struct node));
    n->payload = 10; //random payload
    n->ptr = n;

    printf("%pn", n->ptr);
    foo(2); //increment by 2
    printf("%pn", n->ptr);






    share|improve this answer



























      0














      writing a sample code for your reference. I used similar concept as answered above.



      #include <stdio.h>

      struct node
      int payload;
      void *ptr;
      *n;

      void foo(int x)
      n->ptr = (char *)n->ptr +x;


      int main()
      n = malloc(sizeof(struct node));
      n->payload = 10; //random payload
      n->ptr = n;

      printf("%pn", n->ptr);
      foo(2); //increment by 2
      printf("%pn", n->ptr);






      share|improve this answer

























        0












        0








        0







        writing a sample code for your reference. I used similar concept as answered above.



        #include <stdio.h>

        struct node
        int payload;
        void *ptr;
        *n;

        void foo(int x)
        n->ptr = (char *)n->ptr +x;


        int main()
        n = malloc(sizeof(struct node));
        n->payload = 10; //random payload
        n->ptr = n;

        printf("%pn", n->ptr);
        foo(2); //increment by 2
        printf("%pn", n->ptr);






        share|improve this answer













        writing a sample code for your reference. I used similar concept as answered above.



        #include <stdio.h>

        struct node
        int payload;
        void *ptr;
        *n;

        void foo(int x)
        n->ptr = (char *)n->ptr +x;


        int main()
        n = malloc(sizeof(struct node));
        n->payload = 10; //random payload
        n->ptr = n;

        printf("%pn", n->ptr);
        foo(2); //increment by 2
        printf("%pn", n->ptr);







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 4:54









        Abhinav JainAbhinav Jain

        11




        11



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291982%2fadd-a-certain-number-of-bytes-to-a-void-pointer-in-c%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric