Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9169033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:46:27+00:00 2026-06-17T15:46:27+00:00

I am studying functions that accept arguments of arbitrary datatypes using void pointers. Such

  • 0

I am studying functions that accept arguments of arbitrary datatypes using void pointers. Such a function is the following:

void funct(void *a) {
    float *p = a;
    printf("number = %f\n",*p);
}

Here is a successful invocation to funct:

float x = 1.0f;
funct(&x);

x is declared to be a float and then its pointer, namely &x (which is of type float*) is passed to funct; quite straightforward!

There is however yet another way to declare a variable in C and get its pointer. This is:

float *p;
*p = 1.0f;

But then the call funct(&x); returns a Segmentation fault: 11! How is that possible?

Additionally, assume that I want to create a method that accepts a “number” (i.e. float, integer, double, float or anything else (e.g. even u_short)) and adds 1 to it. What would the most versatile implementation possibly be? Should I consider the following prototype:

void add_one(void* x);

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:46:27+00:00Added an answer on June 17, 2026 at 3:46 pm

    First off, I must make something clear. float *p and float* p are equivalent statements. If you’re a beginner in C, I recommend that you write the latter instead of the former, because it makes clearer what the type is and what the variable is – it will save you a lot of confusion in the future. With that in mind, please remember that in the following block p, not *p, is the variable.

    float* p;
    *p = 1.0f;
    

    The first line allocates a float* on the stack. As it happens with all variables allocated on the stack, its initial value is undefined (most probably garbage). This is not exclusive to pointers: even if you allocate an int and you don’t initialize it, it will have an undefined value.

    The same happens with p, so you should think of p as containing random data initially. If p contains random data and it’s a pointer, it means that it’s pointing to a random address. Attempting (in the second line) to write the value 1.0f (or anything else) to that random address will almost always cause a segmentation fault, because a random address at any given time has little chance of belonging to your program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Total noobie question here. I'm just learning Java, and studying passing arguments to functions.
I was trying to use the function glMultiDrawElements while studying OpenGL (using red book)
I've been studying Scheme recently and come across a function that is defined in
In studying VIM functions to learn to write my own I see that commands
I'm studying this malloc function and I could use some help: static void *malloc(int
I'm studying Javascript using Marijn Haverbeke's book Eloquent JavaScript and didn't understand the following
ok I have this code, that I'm studying class scope{ function printme(){ return hello;
Studying Haskell, i'm trying to code a function takeIf that given a condition and
i studying TDateTime functions and procedure, but not found something that allow me to
i have read articles that say: using the prototype will be fastest since functions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.