-
Is my computer haunted
I am trying to learn about pointers and how to point to an array and how to increment a pointer
when i run this my output is: h,i,j,k,l,m
but i have no h,i,j,k,l,m in my code?!! this does not make sense to me
can anyone explain it?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main() {
char* ptr;
int n;
char string1 [] = { 'A' , 'B' , 'C' , 'D' , 'E' , 'F', 'G', '\0' } ;
n = 3;
ptr = string1;
while ( *ptr != string1[6] ){
printf( "my pointer ptr points at: %c \n", ptr );
ptr++ ; }
printf("%s",string1);
getchar();
}
-
Code:
printf( "my pointer ptr points at: %c \n", ptr );
This is saying to print out a character but you pass in a pointer.
-
Code:
while ( *ptr != string1[6] ){
That's probably not what you want, unless the string never contains duplicate characters.
main() should have a return 0.
-
What's n for? You assign 3 to it, and that's the last and first time it's used.