Home Forums C Programming Understanding Pointers and Strings Re: Re: Understanding Pointers and Strings

#3536
GWILouisaxwzkla
Participant

To understand the problem completely, remember that arrays are configured as contiguous blocks of memory ( the first index in memory precedes the next and so on ) and realize the fact that pointers are simply variables that hold the address of other variables ( the size depends on your hardware and operating system ). Also keep in mind that in C the semantic value of an array’s name is the address of the first item in the array ( array [ 0 ] ). So that in your program you have a situation like :



draw(words + 11, len); //pass in the address of the 11th 8-bit item in memory, words [11]

since the array looks like this in memory ( let 0x0065fd94 be the first address of the array ):

memory address item
0x0065fd94 ‘T’
0x0065fd95 ‘h’
0x0065fd96 ‘i’
0x0065fd97 ‘s’
…………..

and so on your loop:



for(index; *index != *(index + len); index++)
putchar(*index);
}

starts at index == words [ 11 ] which is ‘h’ and continues until ( *index == *(index + len); ) . But since * ( index + len ) is words [ 32 ] == ‘h’ your loop never executes. I think what you wanted to do is this:


#include
#include

void draw(char *w, int len);

int main()
{

char words [ 100 ] = ("This is my hundredth attept st this extremely annoying task :)");
char * index = NULL;
int len = 21;



draw( words + 11, len );



return 0;
}

void draw ( char * w, int len )

{
char * index = NULL;
int i = 0;

index = w;

putchar ( 'n' );
for( ; i < len ; index ++ , i ++ )
{
putchar ( * index );
}
putchar ( 'n' );
}