week 2 c programming code Flashcards
to learn more about c programming
what is a string in c#
string of characters is a 1D array of characters
how much memory does ASCII store
(1 byte) of each character element is stored in
consecutive memory locations
what is string terminated by
String is terminated by the null character ‘\0’ (ASCII value 0)
what is the length of the null string
The null string (length zero) is the null character only
what is use to take input from user in c#
scanf()
what is used to output info to user
printf()
what is the format for string in c
%s
what is the 4 safe string functions
strcpy()
strcat()
strlen()
strcmp
what is strcpy()
copies the string from one destination to another
what do you need to make sure that you do in strcpy()
need to allocate memory to the destination where you want to put your string
e.g
char[10] destination; -> where i will put the string in;
what does strlen do
gets the length of the string;
does strlen take into account the null value
no
what does strcat do
concatenates 2 strings together
in strcat(str1,str2) which string is modified and which stays the same
str2 stays the same and str1 changes as str2 is appended to teh end of str1
what does strcmp do
compares 2 strings and see if they are the same
if strcmp conpares 2 strings and finds them to be equal then what does it return
it returns 0
if strcmp conpares 2 strings and finds them to not be equal then what does it return
returns the numeric difference between the first
non matching characters
or -1 i need to check
what are string problems
Need to ensure that sufficient memory is allocated at run-
time for storing the string - not automatically done by the
compiler
- No checks at run-time (and compile-time), hence common
source of errors - Such “buffer overflows” have been exploited to achieve
execution of code supplied by attackers - Partial solution is to use “safe” functions
what is a partial solution to string problems
- Partial solution is to use “safe” functions
what are safe string functions
strcpy() strcmp , strlen , strcat
what is wrong with this
strncpy(char *dst, char * src, size_t num)
Copies the first num characters of src to dst. If the end of src
(signaled by a null char) is found before num characters have been
copied, dst is padded with zeros until a total of num characters
have been written to it.
No null-character is implicitly appended at the end of dst if src is
longer than num. Thus, in this case, dst shall not be considered a
null terminated C string
what is the definition of a pointer
A pointer is a variable that contains the address of a
variable
pointer is also stored in memory
what is the unary operator*
- is also calld indirection or dereferencing operator
applied to a pointer to accesses the object the pointer points to
e.g c = 5 , *p = &c , d = p now d = 5
what is unary operator &
‘address-of’ operator
points to address of object
p =&c
now p now has address of c so p points to c