Frank said:
Pointer to pointer to char.
Struggling
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int exptab(int n, const char *path)
{
FILE *fin, *fout;
char *path_exp, *exp_suffix = ".exp";
int c;
const int cn = n;
path_exp = malloc(strlen(path) + strlen(exp_suffix) + 1);
if (!path_exp){
fprintf(stderr, "%s: %s\n", __func__, "malloc error");
return EXIT_FAILURE;
}
strcat(strcpy(path_exp, path), exp_suffix);
fout = fopen(path_exp, "w");
free(path_exp);
if (!fout){
fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, "fopen");
return EXIT_FAILURE;
}
fin = fopen(path, "r");
if (!fin){
fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, "fopen");
fclose(fout);
return EXIT_FAILURE;
}
while ((c=fgetc(fin)) != EOF){
n = cn;
if (c == '\t') while (n--) fputc(' ', fout);
else fputc(c, fout);
}
fclose(fin);
fclose(fout);
return 0;
}
/*test*/
#include <errno.h>
int main(int argc, char *argv[])
{
long n;
char *endp;
if (argc != 3){
fprintf(stderr, "%s: <num> <filename>\n", argv[0]);
return -1;
}
errno = 0;
n = strtol(argv[1], &endp, 10);
// print some values
printf(" argv1 is %ld\n" , n);
fprintf(stderr, " stderr has the following error %d\n ", errno);
printf(" errno is %d\n" , errno);
printf(" endp is %p\n" , endp);
printf(" endp points to %p\n" , &endp);
printf(" the char that the pointer to a pointer to char\
called endp points to is %d\n" , & (&endp));
if (*endp || errno){
perror(__func__);
return EXIT_FAILURE;
}
exptab(atoi(argv[1]), argv[2]);
return EXIT_SUCCESS;
}
// gcc k6.c -Wall -o k.exe
F:\gfortran\dan>gcc k5.c -Wall -o k.exe
F:\gfortran\dan>k 555555555555 ot3.txt
argv1 is 2147483647
stderr has the following error 34
errno is 34
endp is 003D3B20
endp points to 0022FF70
main: Result too large
F:\gfortran\dan>type k5.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int exptab(int n, const char *path)
{
FILE *fin, *fout;
char *path_exp, *exp_suffix = ".exp";
int c;
const int cn = n;
path_exp = malloc(strlen(path) + strlen(exp_suffix) + 1);
if (!path_exp){
fprintf(stderr, "%s: %s\n", __func__, "malloc error");
return EXIT_FAILURE;
}
strcat(strcpy(path_exp, path), exp_suffix);
fout = fopen(path_exp, "w");
free(path_exp);
if (!fout){
fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, "fopen");
return EXIT_FAILURE;
}
fin = fopen(path, "r");
if (!fin){
fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, "fopen");
fclose(fout);
return EXIT_FAILURE;
}
while ((c=fgetc(fin)) != EOF){
n = cn;
if (c == '\t') while (n--) fputc(' ', fout);
else fputc(c, fout);
}
fclose(fin);
fclose(fout);
return 0;
}
/*test*/
#include <errno.h>
int main(int argc, char *argv[])
{
long n;
char *endp;
if (argc != 3){
fprintf(stderr, "%s: <num> <filename>\n", argv[0]);
return -1;
}
errno = 0;
n = strtol(argv[1], &endp, 10);
// print some values
printf(" argv1 is %ld\n" , n);
fprintf(stderr, "stderr has the following error %d\n ", errno);
printf(" errno is %d\n" , errno);
printf(" endp is %p\n" , endp);
printf(" endp points to %p\n" , &endp);
//printf(" the pointer to endp points to %d\n" , ??);
if (*endp || errno){
perror(__func__);
return EXIT_FAILURE;
}
exptab(atoi(argv[1]), argv[2]);
return EXIT_SUCCESS;
}
F:\gfortran\dan>gcc k6.c -Wall -o k.exe
k6.c: In function `main':
k6.c:62: error: invalid lvalue in unary `&'
F:\gfortran\dan>
It looked like unary & worked once. Why not twice?