I made the following little program:
#include <stdio.h>
void strncpy(char * s, char * t, int n);
int
main()
{
char string1[]="Learning strings";
char string2[10];
strncpy(string2,string1,3);
printf("string1:%s\nstring2:%s\n",string1,string2);
return 0;
}
void strncpy(char * s, char * t, int n)
{
int i;
for(i=0; i<n && t[i]!=0;i++)
s[i]=t[i];
s[i]=0;
}
I was trying to learn the difference between doing something like:
char greeting[]="Hello!";
And
char * farewell="Goodbye!";
And I thought my program would work with either of the two types of 'strings'(correct way of saying it?), but it only works with the first one.
Why does this happen? What's the difference between the two types?
What would I have to do to my program to be able to use strings of the second type?