pls post the code.
Main Topics
Browse All Topicsi have written a program in c, which compiles and runs ok using borland c++ 4.2.
if i try to compile this program using turbo c 1.5 it produces an error for one line of code:- error: non portable pointer assignment in function main.
this same line of code only produces a warning with c++ :- warning: non portable pointer conversion in function main - and therefore the program compiles and runs ok.
my problem is i need to compile and run it in turbo c.
thanks for any help.(i can supply the prog if required)
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
this is the code- i have indicated the line which is causing the problem.
thanks for any help.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<printer.h>
#define TRUE 1
#define FALSE 0
#define SPACE ' '
#define LINES_PER_PAGE 45
#define STR_SIZE1 sizeof(struct issue_receipt)
#define STR_SIZE2 sizeof(struct rec_delete)
#define STR_SIZE3 sizeof(struct rec_create)
#define STR_SIZE4 sizeof(struct link)
struct issue_receipt{
char rec_type;
char cust_code[6];
char part_num[7];
char ir_quantity[5];
};
struct rec_delete{
char rec_type;
char cust_code[6];
};
struct rec_create{
char rec_type;
char cust_code[6];
char cust_name[21];
char cust_address[61];
char cust_balance[10];
char credit_limit[8];
};
struct link{
char rec_type;
char cust_code[6];
char part_num[7];
char ir_quantity[5];
char cust_name[21];
char cust_address[61];
char cust_balance[10];
char credit_limit[8];
short *next;
};
struct position{
char cust_code[6];
long pos;
}rec_ptr[500];
void totals_calc(FILE *file_ptr,int tot_recs);
void main()
{
FILE *validdat_ptr, *sortdat_ptr;
struct issue_receipt trans_STR;
struct rec_delete del_STR;
struct rec_create create_STR;
struct link *list;
char type, newline='\n', sstore[6];
int rec_num=0,loop=0, loop1=0;
long store;
/*Open files and streams*/
if((validdat_ptr = fopen("A:486981VF.DAT","rb
{
printf("\nERROR OPENING TEST DATA FILE");
exit(1);
}
if((sortdat_ptr = fopen("A:486981SD.DAT","wb
{
printf("\nERROR OPENING SORTED DATA FILE");
exit(1);
}
list = (struct link *)malloc(STR_SIZE4);
rec_ptr[rec_num].pos = list;
/*Main loop*/
while(!feof(validdat_ptr))
{
/*Read first character to identify record type*/
fread(&type,1,1,validdat_p
if(feof(validdat_ptr)) /*Prevents last record from */
break; /*being output twice */
/*Move the pointer back to the start of the record*/
fseek(validdat_ptr,-1,SEEK
type = toupper(type);
/*deal with record*/
switch(type)
{
case 'I':fread(&trans_STR,STR_S
list->next = (struct link *)malloc(STR_SIZE4);
list->rec_type = type;
strcpy(list->cust_code,tra
strcpy(list->part_num,tran
strcpy(list->ir_quantity,t
break;
case 'R':fread(&trans_STR,STR_S
list->next = (struct link *)malloc(STR_SIZE4);
list->rec_type = type;
strcpy(list->cust_code,tra
strcpy(list->part_num,tran
strcpy(list->ir_quantity,t
break;
case 'D':fread(&del_STR,STR_SIZ
list->next = (struct link *)malloc(STR_SIZE4);
list->rec_type = type;
strcpy(list->cust_code,del
break;
case 'C':fread(&create_STR,STR_
list->next = (struct link *)malloc(STR_SIZE4);
list->rec_type = type;
strcpy(list->cust_code,cre
strcpy(list->cust_name,cre
strcpy(list->cust_address,
strcpy(list->cust_balance,
strcpy(list->credit_limit,
break;
default: break;
}
strcpy(rec_ptr[rec_num].cu
rec_num ++;
fread(&newline,1,1,validda
list = list->next;
rec_ptr[rec_num].pos = list;
}
for(loop1=0;loop1<rec_num;
{
for(loop=0;loop<rec_num-1;
{
if(strcmp(rec_ptr[loop].cu
{
store = rec_ptr[loop].pos;
rec_ptr[loop].pos = rec_ptr[loop+1].pos;
rec_ptr[loop+1].pos = store;
strcpy(sstore,rec_ptr[loop
strcpy(rec_ptr[loop].cust_
strcpy(rec_ptr[loop+1].cus
}
}
}
for(loop1=0;loop1<rec_num;
{
list = rec_ptr[loop1].pos; /*****this line is causing the problem *****/
type = list->rec_type;
switch(type)
{
case 'I':trans_STR.rec_type = type;
strcpy(trans_STR.cust_code
strcpy(trans_STR.part_num,
strcpy(trans_STR.ir_quanti
fwrite(&trans_STR,STR_SIZE
break;
case 'R':trans_STR.rec_type = type;
strcpy(trans_STR.cust_code
strcpy(trans_STR.part_num,
strcpy(trans_STR.ir_quanti
fwrite(&trans_STR,STR_SIZE
break;
case 'D':del_STR.rec_type = type;
strcpy(del_STR.cust_code,l
fwrite(&del_STR,STR_SIZE2,
break;
case 'C':create_STR.rec_type = type;
strcpy(create_STR.cust_cod
strcpy(create_STR.cust_nam
strcpy(create_STR.cust_add
strcpy(create_STR.cust_bal
strcpy(create_STR.credit_l
fwrite(&create_STR,STR_SIZ
break;
default :break;
}
fwrite(&newline,1,1,sortda
}
/*rewind(sortdat_ptr); */
rewind(sortdat_ptr);
totals_calc(sortdat_ptr,re
free(list);
fclose(sortdat_ptr);
fclose(validdat_ptr);
}
/*End of main*/
void totals_calc(FILE *file_ptr,int tot_recs)
{
char type,newline='\n';
struct issue_receipt trans_STR;
struct rec_delete del_STR;
struct rec_create create_STR;
while(!feof(file_ptr))
{
/*Read first character to identify record type*/
fread(&type,1,1,file_ptr);
if(feof(file_ptr)) /*Prevents last record from */
break; /* being output twice */
/*Move the pointer back to the start of the record*/
fseek(file_ptr,-1,SEEK_CUR
type = toupper(type);
switch(type)
{
case 'I':fread(&trans_STR,STR_S
fwrite(&trans_STR,STR_SIZE
break;
case 'R':fread(&trans_STR,STR_S
fwrite(&trans_STR,STR_SIZE
break;
case 'D':fread(&del_STR,STR_SIZ
fwrite(&del_STR,STR_SIZE2,
break;
case 'C':fread(&create_STR,STR_
fwrite(&create_STR,STR_SIZ
break;
}
fread(&newline,1,1,file_pt
fwrite(&newline,1,1,stdout
}
}
ozo is right, you mix up types.
the reason why it works under borland compiler is because a pointer can be represented as a long. I often use a long to calculate offsets in a memory buffer.
this should work:
list = (struct link *)rec_ptr[loop1].pos;
but better is to change the type for the pos field in the position struct to struct link *.
also the store variable must be changed to this type.
good luck,
Alexander.
> I often use a long to calculate offsets in a memory buffer
When two pointers to elements of the same array object are subtracted, the result is the
difference of the subscripts of the two array elements. The size of the result is implementation-
defined, and its type (a signed integral type) is ptrdiff_t defined in the <stddef.h> header.
aperdon, ozo: right, but offsetof is only specified for offsets within structs. if one wants to calculate the offset between two arbitrary pointers, this might be more portable than using longs (which usually should be 32bit, where addresses might be wider):
size_t offset = (char *)ptr_a - (char *)ptr_b;
ozo: It's not always undefined, example:
void foo(FILE *f, int a, double b, char c, long d) {
size_t size = ((char *)&d - (char *)&a) + sizeof(long);
fwrite(&a, size, 1, f);
}
has the defined behaviour of writing all relevant data from the parameters to f, plus some optional fill data for alignment. ok, it's a perfectly useless piece of code :-) and I guess we're getting a bit off-topic.
Undefined behavior - behavior, upon use of a nonportable or erroneous program construct,
of erroneous data, or of indeterminately valued objects, for which the standard imposes no
requirements.
ptrdiff_t size = ((char *)&d - (char *)&a)
(not size_t) would be portable if d and a were elements of the same array object, but the layout of the storage for function parameters is unspecified.
to aperdon,
have solved problem! when the pointer to(sortdat_ptr) is passed to the functon it seems to have been lost so the while loop can not find eof. am i right with this? what i have done is replaced rewind(sortdat_ptr) with fclose(sortdat_ptr) and then reopened the file inside the function -file_ptr = fopen etc.
also have used printf as suggested.
regarding initial problem, please would you explain the reason for using struct link *pos and struct link *store.
is pos now replaced with a pointer to pos of data type struct link for example?
many thanks for your help.
Business Accounts
Answer for Membership
by: ozoPosted on 2000-06-05 at 17:13:33ID: 2894699
what is the line of code?