Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to use void pointer in C

2 Answers

0 votes
#include <stdio.h>

int main(void)
{
    int n = 374;

    void* p = &n;

    printf("n = %d\n", n);
    printf("*(int*)p = %d\n", *(int*)p);

    *(int*)p += 222;

    printf("n = %d\n", n);
    printf("*(int*)p = %d\n", *(int*)p);

    n = 100;

    printf("n = %d\n", n);
    printf("*(int*)p = %d\n", *(int*)p);

    return 0;
}





/*
run

n = 374
*(int*)p = 374
n = 596
*(int*)p = 596
n = 100
*(int*)p = 100

*/

 



answered May 12, 2023 by avibootz
edited May 12, 2023 by avibootz
0 votes
#include <stdio.h>

int main(void)
{
    char ch = 'z';

    void* p = &ch;  

    printf("ch = %c\n", ch);
    printf("*(char*)p = %c\n", *(char*)p);

    ch = 'a';

    printf("ch = %c\n", ch);
    printf("*(char*)p = %c\n", *(char*)p);


    *(char*)p = 'r';

    printf("ch = %c\n", ch);
    printf("*(char*)p = %c\n", *(char*)p);

    return 0;
}





/*
run

ch = z
*(char*)p = z
ch = a
*(char*)p = a
ch = r
*(char*)p = r

*/ 

 



answered May 12, 2023 by avibootz

Related questions

1 answer 108 views
1 answer 129 views
129 views asked Jun 21, 2017 by avibootz
1 answer 156 views
156 views asked May 5, 2017 by avibootz
1 answer 111 views
111 views asked Dec 25, 2020 by avibootz
1 answer 96 views
96 views asked May 30, 2023 by avibootz
...