0

Hi iam facing the problem while compiling the below program,

program.h ------------headerfile

typedef struct
{
        unsigned char c ;
        unsigned int i ;
        unsigned long l ;
}data_t ;

program.cc ------c++ file

# include <iostream>
# include <malloc.h>
using namespace std ;

# include "ptr.h"

int main()
{
        data_t data[] =
                        {
                                {'A',1,10},
                                {'B',2,11},
                                {'C',3,12}
                        } ;


        data_t *ptr = &data[0] ;

        data_t *ptr = &data[1] ;

     cout<<"ptr->c"<<ptr->c<<"ptr->i"<<ptr->i<<"ptr->l"<<ptr->l ;

return 0 ;
}

i am getting below errors while compiling

error : redclaration of data_t *ptr
error : data_t *ptr previously declared here

so can i allocate memory for ptr before data_t *ptr = &data[1] ; statement, if this is the case please provide me allocating memory for ptr using new operator.

Edited by pyTony: fixed formatting

2
Contributors
1
Reply
9
Views
5 Years
Discussion Span
Last Post by therockon7throw
0

Hi iam facing the problem while compiling the below program,

program.h ------------headerfile

typedef struct
{
unsigned char c ;
unsigned int i ;
unsigned long l ;
}data_t ;


program.cc ------c++ file

# include <iostream>
# include <malloc.h>
using namespace std ;

# include "ptr.h"

int main()
{
data_t data[] =
{
{'A',1,10},
{'B',2,11},
{'C',3,12}
} ;


data_t *ptr = &data[0] ;

data_t *ptr = &data[1] ;

cout<<"ptr->c"<<ptr->c<<"ptr->i"<<ptr->i<<"ptr->l"<<ptr->l ;

return 0 ;
}

i am getting below errors while compiling

error : redclaration of data_t *ptr
error : data_t *ptr previously declared here


so can i allocate memory for ptr before data_t *ptr = &data[1] ; statement, if this is the case please provide me allocating memory for ptr using new operator.

Hi,

Look at your code below, what are you doing ? See the problem ?

data_t *ptr = &data[0] ;

data_t *ptr = &data[1] ;

You are defining a pointer two times ->

data_t *ptr

pleasa give the second one another name may be

data_t *ptr1

hope it help

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.