-
copying a string
here is my code
Code:
#include <stdio.h>
#define MAX 81
void substring(char *n,int i,int j)
{int k;
char substr[MAX];
for(k=0;k<=j;k++)
{
printf("%c",*(n+i+k));
*substr=*(n+k+i);
substr++;
}
*substr='\0';
printf("%s",substr);
}
int main(void)
{
char name[MAX]; /* allot space */
printf("Hi, what's your name?\n");
gets(name); /* place string into name array */
printf("Nice name, %s.\n", name);
substring(name,2,1);
return 0;
}
its giving an error "error: wrong type argument to increment"
i think i am doing right but still getting error .please someone suggest something
-
substr is an array, not a pointer.
You need to write substr[k] to progress through the array.
Also, read the FAQ on not using gets(), and also pay attention to indentation.
Code:
#include <stdio.h>
#define MAX 81
void substring(char *n, int i, int j)
{
int k;
char substr[MAX];
for (k = 0; k <= j; k++) {
printf("%c", *(n + i + k));
*substr = *(n + k + i);
substr++;
}
*substr = '\0';
printf("%s", substr);
}
int main(void)
{
char name[MAX]; /* allot space */
printf("Hi, what's your name?\n");
gets(name); /* place string into name array */
printf("Nice name, %s.\n", name);
substring(name, 2, 1);
return 0;
}
Finally, use meaningful names in your function declaration, like
void substring(char *name, int startpos, int length)