- When creating a struct no space is allocated
- typedef
- Make a new type name
- Make a new type name
- Typedef double grade
- "grade" is now a new type
- grade g1 = 3.5
- Grade can be used whenever double is used
- Grade can be used whenever double is used
- Typedef unsigned int size_t;
- Typedef void (*_sighandler_t)(int)
- "grade" is now a new type
- Pointers to structs
- Structpoint p1;
- Structpoint* pp;
- pp = &p1;
- the name of a struct is not a pointer to the beginning of the variable
- p1.x = 9
- *pp.x = 9 //doesn't work
- Because dot operator has precedence over *
- Because dot operator has precedence over *
- (*pp).x = 9
- // will properly dereference the struct
- // will properly dereference the struct
- ppàx = 9
- same as (*pp).x =9 only this is proper
- same as (*pp).x =9 only this is proper
- Structpoint p1;
- self referential structures
- if you don't use malloc calloc etc it is put on the stack not the heap
- look in back of the book under IO
- c library IO(buffered)
- this is the buffer which input and output is written to
- int getChar(void)
- int putchar(int)
- scanf
- printf
- c library IO(buffered)
- file access
- FILE
- The way you create one is
- FILE * fp
- fp = fopen("filename", mode);
- mode is
- read write and append
- a 2 character string
- char*
- read write and append
- instead of filename you can use
- "rb"
- "wb"
- Ab"
- "rt"
- "wt"
- "at"
- "rb"
- FILE * fp
//creating a structpoint//
--------------------
structpoint{
int x;
int y;
}
---------------------------
structpoint p1;
p1.x = 5;
p1.y = 6;
------------
or
structpoint p1 = {5,6};
----------------------------------
or
structpoint{
int x;
int y;
}
p1;
p1.x = 5
p1.y = 6;
//you can make a new struct doing it this way
--------------------------------------
struct{
int x;
int y;
}
p1.x = 5;
p1.y = 6;
//cannot make a second struct doing it this way
---------------------------------------
typef unsigned int size_t;
typedef void (*_sighandler_t)(int)
--------------------------------------
typedef structpoint {
int x;
int y;
}
point;
/*now "point" is the name of a type
* it can have pint p1;
*instead of structpoint p1
*/
--------------------------------
/* self referential structures*/
struct listNode{
int value;
struct listNode* next;
}
--------------------------------
struct treeNode{
int value;
struct treeNode* left;
struct treeNode* right;
}
------------------------------------
or
--------------------------------------
typedef structTNode* TreePtr
typedef structTNode{
int value;
TreePtr left;
TreePtr right;
}TreeNode
-----------------------------------------
TreePtr root = (TreePtr)malloc(sizeof(TreeNode)); // - do not do this instead - sizeof(int) + 2*sizeof(TreePtr)
root->value = 5;
root -> right = (TreePtr)malloc(sizeof(TreeNode));
root -> right ->value=2;
root -> right->right =null;
root -> right ->left = null;
/*creates a binaryTree with root starting point at 5 pointing to a 2 on its right side that has no strings after it*/
--------------------
struct item{
double price;
char name[50];
}
item x1;
strcpy(x1,name, "Notebook");
---------------------------
struct item{
double price;
char* name;
}
item x1;
x1.name = "notebook";
-----------------------------------
fprintf(fp,...);
fscanf(fp,...);
fclose(fp);
ferror(fp);
intfeof(fp); //if you reached end of file on that file
fflush(fp);
fseek(fp,...);
fsetpos(fp,...);
--------buffered standard output under this line--
stdin
stdont
stderr
---------unbuffered under this line---
STDIN_FILENO 0
STDOUT_FILENO 1
STDERR_FILENO 2
--------------------------------------------------
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("a");
write(1, "X", 1);
fprintf(stdout, "B");
write (1,"Y", 1);
printf("C");
write(1, "Z\n", 2);
printf("\n");
return 0;
/*output is
XYZ
ABC
this is becuase it's buffered
/*
}