wingnut2626
December 13th, 2013, 01:16 AM
Hey guys. Im working on learning C and I am having some trouble. I think whats going on is that the Book that I'm using to learn this is severly outdated. Basically I am trying to create a string, and print from the first place in the string on to the end (like reading a last name). Here is the code:
#include <stdio.h>
char name[20] = "Roy Hibbert";
int position= 0;
void main(){
while(name[position]!=' '){
position++;
}
printf(&name[position]);
}
and also there is a way to do it using pointers :
#include <stdio.h>
char name[20] = "Derrick Rose";
char *SurnameStart = name;
void main(){
while(*SurnameStart != ' '){
SurnameStart++;
}
SurnameStart++;
printf(SurnameStart);
}
Here is the warning that is generated when I attempt to compile either of these:
warning: format not a string literal and no format arguments [-Wformat-security]
printf(SurnameStart);
What am I doing wrong? And can someone explain to me the importance of pointers anyhow? Not even sure why these are necessary. Thanks in advance
#include <stdio.h>
char name[20] = "Roy Hibbert";
int position= 0;
void main(){
while(name[position]!=' '){
position++;
}
printf(&name[position]);
}
and also there is a way to do it using pointers :
#include <stdio.h>
char name[20] = "Derrick Rose";
char *SurnameStart = name;
void main(){
while(*SurnameStart != ' '){
SurnameStart++;
}
SurnameStart++;
printf(SurnameStart);
}
Here is the warning that is generated when I attempt to compile either of these:
warning: format not a string literal and no format arguments [-Wformat-security]
printf(SurnameStart);
What am I doing wrong? And can someone explain to me the importance of pointers anyhow? Not even sure why these are necessary. Thanks in advance