Try to search your question here, if you can't find : Ask Any Question Now ?

Adresses instead of values

HomeCategory: stackoverflowAdresses instead of values
willson asked 2 weeks ago

I want to read some integers from a file using a structure and a dinamically allocated vector and the code gives me adresses instead of values. Please help, thank you in advance!

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

typedef struct{
    int data;
    int points;
} project;

int main()
    int n,i;
    project *v=(project*)malloc(n*sizeof(project));
    FILE *fp=fopen("","r");
        fscanf(fp,"%d %d",&v[i].data, &v[i].points);
        printf("%d %dn", v[i].data, v[i].points);

edit: In the file I have generated 2*n random numbers using this function:

    void random(int n)
        FILE *f;
        int a=1, b=11,i, c=2, d=10;
        for (i=0;i<n;i++)
            long int r1 = rand()*rand();
            r1 = a+r1%(b-a-1);
            long int r2=rand()*rand();
            r2 = c+r2%(d-c-1);
            fprintf(f, "%ld %ldn",r1,r2);

Also, in the main function I’ve written random(n);.

The adresses shown when I call random(2) are: 9844200 9833784
1684957527 1551071087 and the numbers in the file are 1 2 6 3

1 Answers
Best Answer
Mikhail answered 2 weeks ago
Your Answer

1 + 10 =

Popular Tags

WP Facebook Auto Publish Powered By :