Your code has many compile issues, first you need to define variable j and also using correct braces as follow:
#include <string.h>
#include <stdio.h>
int main(int argc, char **argv) {
if(!fgets(*argv, 64, stdin))
return 0;
for (int i = 0; i < argc; i++)
{
int j = 0;
while(j < strlen(*argv) - 1)
{
if(j == 0)
strcpy(argv + j, argv + j + 1);
j++;
}
}
}
After that, I am strongly against using argv for reading a string because you don't know anything about its size in memory. Also, when you are using the strcpy you must pay attention to the sizes, the destination string should have at least the size of the source.
strlen only works on strings which means char * so using it on char ** is meaningless. The following code copies each string into the left item which I think very similar to what you want to do but please note that string size is very important and strcpy can cause issue.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
const int LEN = 255;
int main(int argc, char **argv) {
int n = 3;
// allocate memory for storing strings
char **strings = malloc(n * sizeof(char *));
// reading strings from user
// and store them
for (int i = 0; i < n; i++) {
strings[i] = malloc(LEN * sizeof(char));
// reads in at most one less than size characters from stream and stores them into the buffer pointed to by s
fgets(strings[i], LEN, stdin);
}
for (int i = 0; i < n - 1; i++) {
// here we consider all the strings has the max size as LEN
// so this copy does not cause error.
strcpy(strings[i], strings[i + 1]);
}
free(strings[n - 1]);
for (int i = 0; i < n - 1; i++) {
printf("[%d] %s", i, strings[i]);
}
}
Also, you can use the non-dynamic way as follows:
#include <string.h>
#include <stdio.h>
#define LEN 255
#define N 3
int main(int argc, char **argv) {
char strings[N][LEN];
// reading strings from user
// and store them
for (int i = 0; i < N; i++) {
// reads in at most one less than size characters from stream and stores them into the buffer pointed to by s
fgets(strings[i], LEN, stdin);
}
for (int i = 0; i < N - 1; i++) {
// here we consider all the strings has the max size as LEN
// so this copy does not cause error.
strcpy(strings[i], strings[i + 1]);
}
for (int i = 0; i < N - 1; i++) {
printf("[%d] %s", i, strings[i]);
}
}
Both programs have the following behavior:
Parham
Ali
Hassan
[0] Ali
[1] Hassan
argv? You can read it into a locally defined char array.