• Welcome to Jose's Read Only Forum 2023.
 

ProgEx18 - C struct; Comparison With PowerBASIC UDT (User Defined Type)

Started by Frederick J. Harris, November 07, 2009, 05:02:03 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frederick J. Harris


/*
  ProgEx18   Careful!  This is a C program, so make sure you save this file as
  Main.c, and do whatever you need to in the development environment you are
  using to create a C Project.

  We havn't covered C structs yet so it is time.  They are exactly the same as
  PowerBASIC types (UDTs) - except naturally the syntax is differnt.  The struct
  in the program below would be exactly the same as this in PowerBASIC...

  Type Employee
    szName     As Asciiz * 32
    szAddress  As Asciiz * 32
    szCity     As Asciiz * 32
    szState    As Asciiz * 32
    szCountry  As Asciiz * 32
    szZipCode  As Asciiz * 32
    iAge       As Dword
    iEmpNum    As Dword
    dblSalary  As Double
  End Type

  Reinforcing the issues we faced in some of the earlier tutorials concerning
  character strings, note how awkward it is to assign the various strings to the
  members of the struct.  The strcpy function needs to be used to copy the bytes
  in the string literals to the storage in the struct emp - which is of type
  Employee.  You just can't assign them directly with the '=' sign as can be
  done with numeric types.

  Another thing to note is how we created an instance of the struct in main().
  For structs you must preface the struct name with the word struct to let C
  know you want a struct.  Compile and run this example and we'll move on and
  discuss this issue further in ProgEx19.
*/

#include <stdio.h>
#include <string.h>

struct Employee
{
char szName[32];
char szAddress[32];
char szCity[16];
char szState[16];
char szCountry[16];
char szZipCode[16];
unsigned int iAge;
unsigned int iEmpNum;
double dblSalary;
};

int main(void)
{
  struct Employee emp;

  strcpy(emp.szName,"Martin Veneski");
  strcpy(emp.szAddress,"1016 Wabash Street");
  strcpy(emp.szCity,"Shamokin");
  strcpy(emp.szState,"PA");
  strcpy(emp.szCountry,"USA");
  strcpy(emp.szZipCode,"17872");
  emp.iAge=23;
  emp.iEmpNum=123456;
  emp.dblSalary=45000;
  printf("emp.szName        = %s\n",emp.szName);
  printf("emp.szAddress     = %s\n",emp.szAddress);
  printf("emp.szCity        = %s\n",emp.szCity);
  printf("emp.szState       = %s\n",emp.szState);
  printf("emp.szCountry     = %s\n",emp.szCountry);
  printf("emp.szZipCode     = %s\n",emp.szZipCode);
  printf("emp.iAge          = %u\n",emp.iAge);
  printf("emp.iEmpNum       = %u\n",emp.iEmpNum);
  printf("emp.dblSalary     = %6.2f\n",emp.dblSalary);
  getchar();

  return 0;
}

/* Output
======================================
emp.szName        = Martin Veneski
emp.szAddress     = 1016 Wabash Street
emp.szCity        = Shamokin
emp.szState       = PA
emp.szCountry     = USA
emp.szZipCode     = 17872
emp.iAge          = 23
emp.iEmpNum       = 123456
emp.dblSalary     = 45000.00

*/

Frederick J. Harris

#1
I overstated the case just a little bit above where I said there is no other way to assign character string data to C structs than by using strcpy(), so I'll show a way that is at times useful.  However, it only works at design time if you have the data and not dynamically at run time.  Here is a little example. This is a C program.  I named it ProgEx18A


#include <stdio.h>

struct Emp
{
char szFirstName[16];
char szLastName[16];
unsigned int id;  
};    

int main(void)
{
 struct Emp emp[]=
 {
  {"Fred","Harris",1},
  {"Marty","Veneski",2},
  {"Joe","Haile",3}
 };
 unsigned int i;
 
 for(i=0; i<3; i++)
     printf("%s\t%s\t\t%u\n",emp[i].szFirstName, emp[i].szLastName, emp[i].id);
 getchar();

 return 0;
}

/*      --Output--
Fred    Harris          1
Marty   Veneski         2
Joe     Haile           3
*/