Problem on assigning values to a structure

following Code segment gives me segmentation fault. Any explanations pls

#include <stdlib.h>
#include <iostream>

typedef struct test{
int value;
int name;
};
using namespace std;
int main(int argc, char** argv) {
struct test * t;
t->value = 8;
cout << t->value << endl;
return (EXIT_SUCCESS);
}
closed account (z05DSL3A)
because t is a pointer to a struct test and you do not initialise it.
try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdlib.h>
#include <iostream>

typedef struct test
{
    int value;
    int name;
};
using namespace std;

int main(int argc, char** argv) 
{
    struct test * t = new test;
    t->value = 8;
    cout << t->value << endl;
    delete(t);
    return (EXIT_SUCCESS);
}
Topic archived. No new replies allowed.