1

I'm trying to make a pointer array which in my case can point to three elements.I want to take the elements at run time and want to access each element but I couldn't. Here is the source code.

#include<stdio.h>

main(){
    int i;
    float *coefficient[3];
    for(i=0;i<=2;i++){
        scanf("%f",coefficient[i]);
    }
    for(i=0;i<=2;i++){
        printf("%f\n",*coefficient[i]);
    }
}
2
  • coefficient is already essentially a pointer. Try declaring float coefficient[3]; instead of float *coefficient[3] Commented Jan 17, 2020 at 20:36
  • 3
    @RobertHarvey: They'd also need to remove the * in the printf, and change the scanf to scanf("%f", &coefficient[i]); to match. I suspect they declared a pointer array solely because they didn't understand how you're supposed to pass arguments to scanf (using & to get the address of an existing stack variable typically). Commented Jan 17, 2020 at 20:38

3 Answers 3

4

Change

float *coefficient[3];

to

 float coefficient[3];

As you need an array of float not float pointers

Then,

change

scanf("%f",coefficient[i]);

to

scanf("%f",&coefficient[i]);

As you need a pointer to the float to read in. Please also check the return value from scanf

As you have an array of floats

printf("%f\n",coefficient[i]);

is what you require

Sign up to request clarification or add additional context in comments.

Comments

1

Your pointers are uninitialized, so dereferencing them causes undefined behavior. You need to allocate memory for them, e.g. using malloc().

    for(i=0;i<=2;i++){
        coefficient[i] = malloc(sizeof *coefficient[i])
        scanf("%f",coefficient[i]);
    }

And after you print them, you should use free() to release the memory.

Comments

1

For starters according to the C Standard the function main without parameters shall be declared like

int main( void )

You declared an array of three pointers but the elements of the array are not initialized and have indeterminate values.

As a result calling the function scanf in this loop

for(i=0;i<=2;i++){
    scanf("%f",coefficient[i]);
}

invokes undefined behavior.

The pointers which are elements of the array shall point to a valid memory where data may be stored using the pointers.

For example you can for each pointer dynamically allocate memory that will be pointed to by the corresponding pointer

Here is a demonstrative program.

#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
    enum { N = 3 };
    float  *coefficient[N];

    for ( size_t i = 0; i < N; i++ )
    {
        coefficient[i] = malloc( sizeof( float ) );
    }

    for ( size_t i = 0; i < N; i++ )
    {
        scanf( "%f", coefficient[i] );
    }

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%f\n", *coefficient[i] );
    }

    for ( size_t i = 0; i < N; i++ )
    {
        free( coefficient[i] );
    }

    return 0;
}

If to enter

1.1
2.2
3.3

then the output will look like

1.100000
2.200000
3.300000

You can format the output as you like.

In general you should check that a call of malloc and/or of scanf was successful.

1 Comment

It seems like I have lately seen more questions (and answers!) using int main() {...} than ever before, and here OP code doesn't seem to acknowledge main() returning a value at all. Hmmm. Maybe it's a retro computing thing. IAC, it might be helpful to point out that more robust code checks for failure of malloc() and checks the value returned by scanf().

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.