0% found this document useful (0 votes)
4 views

C Programming Week10

The document provides an overview of accessing arrays with pointers in C programming, including examples of pointer arithmetic and string manipulation. It explains the relationship between arrays and pointers, highlighting that array names are not pointers and the use of void pointers. Additionally, it covers string handling, copying strings, and the equivalence of array indexing and pointer arithmetic.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C Programming Week10

The document provides an overview of accessing arrays with pointers in C programming, including examples of pointer arithmetic and string manipulation. It explains the relationship between arrays and pointers, highlighting that array names are not pointers and the use of void pointers. Additionally, it covers string handling, copying strings, and the equivalence of array indexing and pointer arithmetic.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

30/10/17

Accessing Arrays with Pointers


#include <stdio.h>
int myArray[ ] = {1,24,17,4,-5,100};
int *ptr;
int main(void){
CS1100 int i;
Introduction to Programming ptr = &myArray[0];
printf(“\n”);
Pointers for (i = 0; i < 6; i++){
printf("myArray[%d] = %d ", i, myArray[i]);
Madhu Mutyam
Department of Computer Science and Engineering
printf(“value at ptr + %d is %d\n", i, *(ptr + i));
Indian Institute of Technology Madras }
return 0;
Course Material – SD, SB, PSK, NSN, DK, TAG – CS&E, IIT M 1 } 2
SD, PSK, NSN, DK, TAG – CS&E, IIT M

ptr++ and ++ptr *ptr++


• ++ptr and ptr++ are both equivalent to ptr + 1 • *ptr++ is to be interpreted as returning the value
– though they are “incremented” at different times pointed to by ptr and then incrementing the
• Replace the following statement pointer value.
printf(“value at ptr + %d is %d\n", i, *(ptr + i)); • This has to do with the precedence of the
with: operators.
printf("ptr + %d = %d\n",i, *ptr++); • (*ptr)++ would increment, not the pointer, but
printf("ptr + %d = %d\n",i, *(++ptr)); that which the pointer points to!
– i.e. if used on the first character of the example string
“IIT” the ‘I’ would be incremented to a ‘J’.

SD, PSK, NSN, DK, TAG – CS&E, IIT M 3 SD, PSK, NSN, DK, TAG – CS&E, IIT M 4

Arrays Array Names Are Not Pointers


• The name of the array is the address of the first • While we can write
element in the array ptr = myArray;
• In C, we can replace • we cannot write
ptr = &myArray[0]; myArray = ptr;
with • The reason:
ptr = myArray; – While ptr is a variable, myArray is a constant
to achieve the same result – That is, the location at which the first element of
myArray will be stored cannot be changed once
• Many texts state that the name of an array is a
myArray has been declared
pointer
SD, PSK, NSN, DK, TAG – CS&E, IIT M 5 SD, PSK, NSN, DK, TAG – CS&E, IIT M 6

1
30/10/17

Pointer Types Trying Out Pointers


• C provides for a pointer of type void. We can #include <stdio.h>
declare such a pointer by writing: int j = 1, k = 2; int *ptr;
void *vptr; main( ) {
Generic address of j
ptr = &k;
• A void pointer is a generic pointer printf(“\n j has the value %d and is stored at %p”,j,(void*)&j);
– For example, a pointer to any type can be compared printf(“\n k has the value %d and is stored at %p”,k,(void*)
to a void pointer &k);
• Type casts can be used to convert from one type printf(“\n ptr has the value %p stored at %p”, ptr, (void *)
of pointer to another under proper circumstances &ptr);
printf(“\nThe value of the integer pointed to by ptr is %d\n”,
*ptr);
} Dereferencing – will print r-value of k
SD, PSK, NSN, DK, TAG – CS&E, IIT M 7 SD, PSK, NSN, DK, TAG – CS&E, IIT M 8

Pointers and Strings A Character Array


• C does not have a string type • One could create a string as follows,
– languages like Pascal, Fortran have… char myString[40];
• In C, a string is an array of characters terminated myString[0] = 'T';
with a binary zero character (written as '\0’) myString[1] = 'e';
– remember this is not the character ‘0’
myString[2] = 'd’;
• One can manipulate strings as character arrays
myString[3] = '\0’;
• Character arrays can also be accessed by pointers – Note - terminated with a nul character
• nul (or ‘\0’) ≠ NULL (pointer to nothing)
• Obviously this is very tedious
9 Ted Jenson’s tutorial on pointers 10
SD, PSK, NSN, DK, TAG – CS&E, IIT M SD, PSK, NSN, DK, TAG – CS&E, IIT M http://pweb.netcom.com/~tjensen/ptr/cpoint.htm

“Strings” Strings: Input and Output


• One might write: • The function gets( ) accepts the name of the
char myString[40] = {'T', 'e', 'd', '\0’}; string as a parameter, and fills the string with
• But this also takes more typing than is convenient characters that are input from the keyboard till
newline character is encountered. At the end, a
• So, C permits: null terminator is appended.
char myString[40] = "Ted"; – Not a popular function because there are no built-in
– Note that C automatically inserts ‘\0’ checks
• Compiler sets aside a contiguous block of memory • char *gets(char *s);
40 bytes long • gets(str) – reads from standard input into str
• The first four bytes contain Ted\0 • puts(str) – writes to standard output from str
SD, PSK, NSN, DK, TAG – CS&E, IIT M 11 SD, PSK, NSN, DK, TAG – CS&E, IIT M 12

2
30/10/17

gets may Overwrite Memory Character Pointers


char b1[] = “ABCDE”; #include <stdio.h>
char b2[] = “LMNOF”; char strA[80] = "A string to be used for demonstration";
char b3[] = “ZYXWV”; char strB[80];
puts(b1); int main(void)
puts(b2); A sample run Another run {
puts(b3); puts(b1); ABCDE puts(b1); ABCDE char *pA; /* a pointer to type character */
puts(“Input:”); puts(b2); LMNOP puts(b2); LMNOP char *pB; /* another pointer to type character */
gets(b2); puts(b3); ZYXWV puts(b3); ZYXWV puts(strA); /* show string A */
puts(…); Input: 1234 puts(…); Input: 1234567890 pA = strA; /* point pA at string A */
puts(b1);
gets(b2); gets(b2); puts(pA); /* show what pA is pointing to */
puts(b2);
puts(b3); puts(b1); ABCDE puts(b1); 7890
puts(b2); 1234 puts(b2); 1234567890 --continued à
13 14
puts(b3); ZYXWV
SD, PSK, NSN, DK, TAG – CS&E, IIT M puts(b3); ZYXWV SD, PSK, NSN, DK, TAG – CS&E, IIT M

Copying Strings… A Version of strcpy


char *myStrcpy(char *destination, char *source)
pB = strB; /* point pB at string B */
{
putchar('\n'); /* move down one line on the screen */
char *p = destination;
while(*pA != '\0') /* while string */
while (*source != '\0')
{
{
*pB++ = *pA++; /* copy and increment pointer */
*p++ = *source++;
}
}
*pB = '\0'; /* insert end-of-string */
*p = '\0';
puts(strB); /* show strB on screen */
return destination;
return 0;
}
}
Ted Jenson’s tutorial on pointers Ted Jenson’s tutorial on pointers
http://pweb.netcom.com/~tjensen/ptr/cpoint.htm http://pweb.netcom.com/~tjensen/ptr/cpoint.htm

SD, PSK, NSN, DK, TAG – CS&E, IIT M 15 SD, PSK, NSN, DK, TAG – CS&E, IIT M 16

Equivalent Definition Using Arrays Copying Arrays Using Pointers


char *myStrcpy(char dest[], char source[]) • Exercise – define a function to copy part of an
{ char *myStrcpy(char *destination, char *source) integer array into another. Access the elements
int i = 0; char *p = destination; using pointers.
while (source[i] != '\0') while (*source != '\0') • Function prototype:
{
void intCopy(int *ptrA, int *ptrB, int num);
dest[i] = source[i];
i++; *p++ = *source++; – where num is the number of integers to be copied.
}
dest[i] = '\0'; *p = '\0’;
return dest; return destination;
}
SD, PSK, NSN, DK, TAG – CS&E, IIT M 17 SD, PSK, NSN, DK, TAG – CS&E, IIT M 18

3
30/10/17

Pointer Arithmetic ≡ Array Indexing Indexes are Converted to Pointer Addresses


• Both work identically • We could write *(i + a) just as easily as *(a + i).
• Since parameters are passed by value, in both the • But *(i + a) could have come from i[a] !
passing of a character pointer or the name of the • From all of this comes the curious truth that if:
array as above, what actually gets passed is the
char a[20]; int i;
address of the first element of each array.
Writing a[3] = ‘x’; is the same as writing
• The numerical value of the parameter passed is
the same. This would tend to imply that somehow 3[a] = ‘x’; !
source[i] is the same as *(source+i).

Ted Jenson’s tutorial on pointers


SD, PSK, NSN, DK, TAG – CS&E, IIT M 19 SD, PSK, NSN, DK, TAG – CS&E, IIT M http://pweb.netcom.com/~tjensen/ptr/cpoint.htm 20

Equivalent Statements Declaring “IITM”


dest[i] = source[i]; • char myName[40] = “IITM”;
• due to the fact that array indexing and pointer – would allocate space for a 40 byte array and put the
arithmetic yield identical results, we can write string in the first 5 bytes
this as: • char myName[] = “IITM”;
*(dest + i) = *(source + i); – the compiler would count the characters, leave room
for the nul character and store the total of the 5
• Also we could write characters in the memory location named by myName
while (*source != '\0’) as • char *myName = “IITM”;
while (*source) – in the pointer notation, the same 5 bytes required,
– since the code for ‘\0’ is 0 = false plus 4 bytes to store the pointer variable myName

SD, PSK, NSN, DK, TAG – CS&E, IIT M 21 SD, PSK, NSN, DK, TAG – CS&E, IIT M 22

You might also like