mercredi 30 novembre 2016

C++11 Pointers Arrays and Pointers & Pointers to a string

Struggling with Pointers if its not too much to ask can someone run me through this thanks !

1.Consider the method sum, below, which computes the sum of values in an array of ints.

int sum(int a[], int size)
{
 int total = 0;
 for (int i=0; i <size; i++)
 {
 total = total + a[i];
 }
 return total;
}

Now that you know that the int a[] is effectively a pointer, modify the method so that it can accept a pointer to the array a and modify the method body so that it increments the pointer, in the knowledge that a point p initially pointd to a[0].

2."Given the string "A string." Print on a single line to the console, the letter located at the
index 0, the pointer position of this letter and the letter ‘t’ from the String. Update the pointer to pointer +2. Finally, on a separate line, print the pointer and the letters r and g

from the string (using the pointer)."

Ever since getting onto pointers I have been struggling ! is anyone able to help me out ?

Thanks !

Aucun commentaire:

Enregistrer un commentaire