Tag Archives: programming

12.03.2015 lab notes continues

//struct and typedef
#include <stdio.h>
#include <string.h>
/*
There’s two different situations here depending on
whether we dynamically feed the struct or not.In this
piece of code lets check out how we can use struck in manual memory management
*/
typedef char abc;
//What i did here with typedef is that i took data type char and defined it with another name in this case “abc”
typedef struct entry
{
abc name[15];
abc surname[15];
unsigned short int age;

}ENTRY;

ENTRY a,b;

/*
struct entry
{
abc name[15];
abc surname[15];
abc adress[50];
unsigned short int age;

}abraham,bob;

abraham and bob are variables here in type of entry(that’s what we chose to call our struct,if you
pull your mouse onto abraham(in Visual Studio) you’ll see “entry abraham” but
if i write my struct column like this:

typedef struct entry
{
abc name[15];
abc surname[15];
abc adress[50];
unsigned short int age;

}ENTRY;

Then “ENTRY” here would be the name of struct we just create(if you ask why do this instead of the other version,
once we do this we don’t have to write:
struct entry new_entry; to create variable “new_entry”.
All we have to is this:
ENTRY new_entry; )
and if you
pull your mouse onto ENTRY (in Visual Studio) you’ll see “typedef entry ENTRY”.

*/
int main(){
strcpy_s(a.name,”Andrew”);
strcpy_s(a.surname,”Lincoln”);
a.age=35;
strcpy_s(b.name,”Bob”);
strcpy_s(b.surname,”Birch”);
b.age=60;
//check
printf(“***Entry A***\nName:%s\nSurname:%s\nAge:%d\n”,a.name,a.surname,a.age);
printf(“***Entry B***\nName:%s\nSurname:%s\nAge:%d”,b.name,b.surname,b.age);
getchar();
return 0;
}

Here’s the output:

Adsız2

 

 

 

 

 

//And this is the one with dynamic mem.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct record
{
char name[15];
char surname[15];
unsigned short int age;

}RECORD;

RECORD * p=(RECORD*)malloc(sizeof(RECORD));

int main(){
strcpy_s(p->name,”Francis”);
strcpy_s(p->surname,”Underwood”);
p->age=60;
//check
printf(“***Record***\nName:%s\nSurname:%s\nAge:%d”,p->name,p->surname,p->age);
getchar();

free(p);
return 0;
}

Here’s the output:

3

Dynamic Memory Allocation(in C)-12.03.2015

I’ve decided to keep my “int. to programming” lab’s source files  as blog entries so that i can reach out more easily and that you guys might find them useful as well as my schoolmates.So here’s the source file from today’s class:

#include <stdio.h>
/*dynamic memory allocation functions malloc(),calloc(),realloc()
and free() are defined in stdlib.h library.
malloc(int size)
calloc(int count,size)
realloc(void*pointer,size):you can’t use realloc() function unless you used malloc() or calloc() before.
While these three above for memory assignment free() is a function used to return the memory space.
*/
#include <stdlib.h>
int main(){
int * ptr;
ptr=(int *)calloc(5,sizeof(int));
if(ptr!=NULL)
{
*ptr=1;
*(ptr+1)=2;
//*ptr and ptr[0] are same things.
ptr[2]=4;
ptr[3]=8;
ptr[4]=16;
}
ptr=(int *)realloc(ptr,7*sizeof(int));
if(ptr!=NULL)
{
ptr[5]=32;
ptr[6]=64;
}
//Printing ptr to see if it works.
for(int i=0;i<6;i++){
printf(“ptr[%d]=%d\n”,i,ptr[i]);
}
/*if we don’t use free() function then the memory occupied
by functions realloc and calloc
can’t be used again which isn’t exactly what we want from dynamic memory management functions. */
free(ptr);

getchar();
return 0;
}

Here’s the output:

Adsız