SandBag_1996
SandBag_1996

Reputation: 1601

accessing structure elements through pointer

My program has a structure like this:

struct point
{
  int x;
  int y;
}*ptt;

struct point *pointer(int c, int d)
{
  ptt->x = c;
  ptt->y = d;
  return ptt;
}
int main ()
{
  struct point *pqq;
  pqq = ptt;         // Not too sure about this
  pqq = pointer(100,123);
  printf("Pointer value is %d\n",pqq->x);
  return 0;
}

Now the program crashes during the call to pointer. I suspect that the way I am initializing x, and y like ptt->x is wrong.. but I am not too sure about the exact way of initializing them. What is the problem here?

Upvotes: 2

Views: 5989

Answers (5)

burkina faso
burkina faso

Reputation: 11

One way of fixing it would be this way: So the problem that i found was that after you define the strut "point" you declare a pointer-to-struct "*ptt" but you don't initialize it. So ptt points to nothing; let's say ptt points to garbage. So further down in the main function you declare "*pqq" and initialize to the address that ptt points to. So you declare "*pqq" and tell it to point to the same address ptt points to right at this statement "pqq = ptt;". But don't forget that ptt originally didn't point to anything. So now you have both ptt and pqq pointing to garbage. When you call the function here " pqq = pointer(100,123);" the function uses ptt to access its members x and y directly. But again ptt points to nothing. So the function accesses nothing therefore returns nothing into pqq which also pointed to nothing. So the program crashes because this is not allowed. you can't return anything since you're not accessing anything. So my fix is: when you declare the pointer-to-struct, initialize it dynamically before you use it. There is the fixed code with comments:

#include <stdio.h> // header that defines the standard input and output routines
#include <stdlib.h> // header to use malloc()

//struct definition
struct point
{
int x;
int y;
}*ptt = NULL;   // it's safe to first initialize pointers to NULL

//function definition
struct point *pointer(int c, int d)
{
ptt->x = c;
ptt->y = d;
return ptt;
}

int main()
{//main

   ptt = (struct point*) malloc(sizeof( struct point)); //malloc() creates a space big enough to hold a variable of type "struct point" this way " malloc(sizeof( struct point))".
                                                        // But malloc() returns a pointer to void (void*) as return value
                                                        // so i type cast that value returned by malloc()  into a pointer-to-struct this way "(struct point*)"
   struct point *pqq = NULL; // it's safe to first initialize pointers to NULL
   pqq = ptt;         //  now ptt points to the memory location given by the malloc function and pqq points to the same place as well
   pqq = pointer(100, 123);
   printf("Pointer value is %d\n", pqq->x);

   free(ptt); // don't forget to free memory. You only need to free one pointer. Whichever one of them.
              // Because both ptt and pqq point to the same memory location so by freeing one, you automatically free the other;
             // Don't attempt to free both at the same time because that would generate a run time error since the C language standard does not allow to free the same memory location twice.

   return 0;

}//end main

//Sorry for the long comment but i wanted to explain it in detail. Hope it helps.

Upvotes: 1

sr01853
sr01853

Reputation: 6121

If you are not allowed to change the structure and pointer function, small change in main() should work

int main ()
{
 struct point *pqq;     

/* ptt is a global pointer visible to main and malloc returns a valid address of type struct point with out which you can not assign a value to its variable */
 ptt = malloc(sizeof(struct point));   

/* pqq = ptt is not necessary - the below statement does that already  */
 pqq = pointer(100,123);
 printf("Pointer value is %d\n",pqq->x);
 return 0; 
 }

Upvotes: 3

nagaradderKantesh
nagaradderKantesh

Reputation: 1690

use as below:

int main ()
{
   struct point p;
   ptt = &p;
   struct point *pqq;
   pqq = pointer(100,123);
   printf("Pointer value is %d\n",pqq->x);

   return 0;
 }

your code was showing error at ptt->x because you are using pointer to a structure variable "ptt" without initializing. you should initialize that using structure variable, so that it is pointing to the structure , then you can access the members of the structure using pointer variable i.e ptt.

Upvotes: 1

askmish
askmish

Reputation: 6674

You should allocate memory to the pointers before using them and free when you no longer need to use that pointer.Please find my comments inline with the code:

int main ()
{
  struct point *pqq=NULL;//Good practice to assign uninitialized pointers with a NULL
  //Before using the ptt pointer allocate memory
  ptt=malloc(sizeof(struct point));
  //Handle the memory allocation failed error
  ptt->x=ptt->y=0;//Good practice
  pqq = ptt;//both pqq and ptt point to same allocated address          
  pqq = pointer(100,123);//And this statement makes the earlier statement pqq=ptt useless. :)
  printf("Pointer value is %d\n",pqq->x);
  free(pqq);
  free(ptt);
  return 0;
}

Hope this helps you.

Upvotes: 1

Bharath Mg
Bharath Mg

Reputation: 1127

Online compiler SCreenshot You have to allocate memory for pointers.

 struct point
  {
      int x;
     int y;
  }*ptt;

 struct point *pointer(int c, int d)
 {
   ptt=malloc(10);
   ptt->x = c;
   ptt->y = d;
 return ptt;
}
int main ()
{
  struct point *pqq;        
 pqq = pointer(100,123);
 printf("Pointer value is %d\n",pqq->x);
 return 0; 
 }

Upvotes: 0

Related Questions