Question

portability

Asked by: spark60

i 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.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2000-06-05 at 16:12:37ID10397537
Tags

pointer

,

assignment

,

function

Topic

C Programming Language

Participating Experts
5
Points
50
Comments
20

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

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.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

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.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

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.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. portability issues
    What are the issues of portability on Unix and NT? I want to make a my code portable on Unix as well as windows platform.
  2. Portable data types in C++
    Do you know of some data type definitions made for portability of data types between the different platforms for example Linux, Windows, VMS and SUN Solaris?
  3. Import data from PC to portable PC
    I work on my PC, but sometimes have to go outside with my portable PC, and I want to have the same data in both of them. is there any way to update the contacts, reminders,... etc. from the PC to the portable PC? Thanks!

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

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.

Join the Community

Answers

 

by: ozoPosted on 2000-06-05 at 17:13:33ID: 2894699

what is the line of code?

 

by: rbrPosted on 2000-06-05 at 23:16:11ID: 2895732

pls post the code.

 

by: spark60Posted on 2000-06-06 at 03:34:30ID: 2896803

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")) == NULL)
  {
     printf("\nERROR OPENING TEST DATA FILE");
     exit(1);
  }

  if((sortdat_ptr = fopen("A:486981SD.DAT","wb+")) == NULL)                      
  {
     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_ptr);

     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_CUR);
     type = toupper(type);

        /*deal with record*/
     switch(type)
     {

        case 'I':fread(&trans_STR,STR_SIZE1,1,validdat_ptr);
                    list->next = (struct link *)malloc(STR_SIZE4);
                    list->rec_type = type;
                    strcpy(list->cust_code,trans_STR.cust_code);
                    strcpy(list->part_num,trans_STR.part_num);
                    strcpy(list->ir_quantity,trans_STR.ir_quantity);
                    break;

        case 'R':fread(&trans_STR,STR_SIZE1,1,validdat_ptr);
                    list->next = (struct link *)malloc(STR_SIZE4);
                    list->rec_type = type;
                    strcpy(list->cust_code,trans_STR.cust_code);
                    strcpy(list->part_num,trans_STR.part_num);
                    strcpy(list->ir_quantity,trans_STR.ir_quantity);
                    break;

        case 'D':fread(&del_STR,STR_SIZE2,1,validdat_ptr);
                    list->next = (struct link *)malloc(STR_SIZE4);
                    list->rec_type = type;
                    strcpy(list->cust_code,del_STR.cust_code);
                    break;

        case 'C':fread(&create_STR,STR_SIZE3,1,validdat_ptr);
                    list->next = (struct link *)malloc(STR_SIZE4);
                    list->rec_type = type;
                    strcpy(list->cust_code,create_STR.cust_code);
                    strcpy(list->cust_name,create_STR.cust_name);
                    strcpy(list->cust_address,create_STR.cust_address);
                    strcpy(list->cust_balance,create_STR.cust_balance);
                    strcpy(list->credit_limit,create_STR.credit_limit);
                    break;

        default: break;
     }
     strcpy(rec_ptr[rec_num].cust_code,list->cust_code);
     rec_num ++;


     fread(&newline,1,1,validdat_ptr);    /*Read the newline after each record*/


     list = list->next;


     rec_ptr[rec_num].pos = list;
  }


  for(loop1=0;loop1<rec_num;loop1++)
  {
     for(loop=0;loop<rec_num-1;loop++)
     {
        if(strcmp(rec_ptr[loop].cust_code,rec_ptr[loop+1].cust_code) > 0)
        {
          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].cust_code);
          strcpy(rec_ptr[loop].cust_code,rec_ptr[loop+1].cust_code);
          strcpy(rec_ptr[loop+1].cust_code,sstore);
        }
     }
  }


  for(loop1=0;loop1<rec_num;loop1++)
  {
     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,list->cust_code);
                    strcpy(trans_STR.part_num,list->part_num);
                    strcpy(trans_STR.ir_quantity,list->ir_quantity);
                    fwrite(&trans_STR,STR_SIZE1,1,sortdat_ptr);
                    break;

        case 'R':trans_STR.rec_type = type;
                    strcpy(trans_STR.cust_code,list->cust_code);
                    strcpy(trans_STR.part_num,list->part_num);
                    strcpy(trans_STR.ir_quantity,list->ir_quantity);
                    fwrite(&trans_STR,STR_SIZE1,1,sortdat_ptr);
                    break;

        case 'D':del_STR.rec_type = type;
                    strcpy(del_STR.cust_code,list->cust_code);
                    fwrite(&del_STR,STR_SIZE2,1,sortdat_ptr);
                    break;

        case 'C':create_STR.rec_type = type;
                    strcpy(create_STR.cust_code,list->cust_code);
                    strcpy(create_STR.cust_name,list->cust_name);
                    strcpy(create_STR.cust_address,list->cust_address);
                    strcpy(create_STR.cust_balance,list->cust_balance);
                    strcpy(create_STR.credit_limit,list->credit_limit);
                    fwrite(&create_STR,STR_SIZE3,1,sortdat_ptr);
                    break;

        default :break;
     }

     fwrite(&newline,1,1,sortdat_ptr);
  }

  /*rewind(sortdat_ptr); */
  rewind(sortdat_ptr);
  totals_calc(sortdat_ptr,rec_num);

  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_SIZE1,1,file_ptr);
                                      fwrite(&trans_STR,STR_SIZE1,1,stdout);
                    break;
        case 'R':fread(&trans_STR,STR_SIZE1,1,file_ptr);
                                      fwrite(&trans_STR,STR_SIZE1,1,stdout);
                    break;
        case 'D':fread(&del_STR,STR_SIZE2,1,file_ptr);
                                      fwrite(&del_STR,STR_SIZE2,1,stdout);
                    break;
        case 'C':fread(&create_STR,STR_SIZE3,1,file_ptr);
                                      fwrite(&create_STR,STR_SIZE3,1,stdout);
                    break;
     }
     fread(&newline,1,1,file_ptr);
     fwrite(&newline,1,1,stdout);
  }

}


 

by: ozoPosted on 2000-06-06 at 03:53:16ID: 2896893

list is a pointer and rec_ptr[loop1].pos is a long
why are you trying to assign a long to a pointer?
struct link *list;
struct position{
             char cust_code[6];
             long pos;
             }rec_ptr[500];

 

by: aperdonPosted on 2000-06-06 at 06:17:59ID: 2897763

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.

 

by: serg111Posted on 2000-06-06 at 11:28:38ID: 2900452

Go to Borland "settings" and you can change which expressions are considered to errors or just a warnings

 

by: ozoPosted on 2000-06-06 at 13:51:09ID: 2901273

> 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.

 

by: aperdonPosted on 2000-06-06 at 15:04:27ID: 2901670

to ozo:

imagine the following struct:

typedef struct
{
   int i;
   char c;
   long l;
} myType;

you can calculate the offset of field l in the struct in the following way.

myType m;
long offset = (long)(&m.l) - (long)&m;

 

by: ozoPosted on 2000-06-06 at 15:21:39ID: 2901739

The portable way to do that is

#include <stddef.h>
size_t offset = offsetof(myType,l);

 

by: fremsleyPosted on 2000-06-07 at 02:43:58ID: 2903962

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;

 

by: ozoPosted on 2000-06-07 at 03:03:26ID: 2904033

Unless both pointers point to elements of the same array object, or one past the last element of the array object, the behavior is undefined.

 

by: spark60Posted on 2000-06-07 at 03:46:44ID: 2904194

to aperdon:

i have changed the type for the pos field in the position struct to struct link *pos(also struct link *store) and code compiles ok but when i run the prog it just loops in the function totals_calc and there is no output to screen. any ideas on this?

thanks to all for your continued help.

 

by: fremsleyPosted on 2000-06-07 at 05:47:57ID: 2904740

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.

 

by: ozoPosted on 2000-06-07 at 08:50:23ID: 2906138

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.

 

by: aperdonPosted on 2000-06-07 at 13:50:44ID: 2908184

Add some printf's in the while-loop.

 

by: fremsleyPosted on 2000-06-07 at 23:14:36ID: 2910287

ozo> [...] but the layout of the storage for function parameters is unspecified

No. This is specified:

  void foo(int a, int b, int c) {
    int *arr = &a;
    /* ... */
  }

then:

  arr[0] -> a
  arr[1] -> b
  arr[2] -> c

otherwise the parameter ellipsis (...) would not be usable.

 

by: ozoPosted on 2000-06-07 at 23:45:41ID: 2910499

fremsley, a particular implementation is allowed to define it that way, in which case it must define the appropriate va_start and va_arg  macros in <stdarg.h> to make parameter ellipsis useable,
but the C standard imposes no such requirement.

 

by: fremsleyPosted on 2000-06-08 at 02:48:34ID: 2911324

ozo: I checked the header files; looks like you're right. I just couldn't believe still to learn something new about C after using for some 12 years :-)

 

by: spark60Posted on 2000-06-08 at 03:14:33ID: 2911447

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.

 

by: spark60Posted on 2000-06-15 at 12:39:23ID: 2956786

Comment accepted as answer

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...