Home > Net >  Is printf("%s", *string) possible?
Is printf("%s", *string) possible?

Time:05-02

My professor told me to make a program which reads names, ages, antiquity and docket of X quantity of people, and when it's done, print it on a table-like format. For all of that, I have to have 2 separate functions. One that reads what the user inputs, and other that outputs everything the user inputted in a table like format.

Now, the problem is that when I try to print the gathered info, it only prints the first character of every "slot" on the array. Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define sfb if(db[i][0][0] == 17 || db[i][1][0] == 17 || db[i][2][0] == 17 || db[i][3][0] == 17){break;}

void read();
void printt(char *, int);

int main(){
    read();
    return 0;
}

void read(){
    char db[20][4][20];
    memset(db, 0, sizeof db);
    printf("Ingrese ^Q para salir\n");
    int i;
    for(i = 0; i < 20; i  ){
        printf("Ingrese nombre de persona: ");
        fgets(db[i][0], sizeof db[i][0], stdin);
        db[i][0][strcspn(db[i][0], "\n")] = 0;
        sfb;
        printf("Ingrese legajo de %s: ", db[i][0]);
        fgets(db[i][1], sizeof db[i][1], stdin);
        db[i][1][strcspn(db[i][1], "\n")] = 0;
        sfb;
        printf("Ingrese edad de %s: ", db[i][0]);
        fgets(db[i][2], sizeof db[i][2], stdin);
        db[i][2][strcspn(db[i][2], "\n")] = 0;
        sfb;
        printf("Ingrese antiguedad de %s: ", db[i][0]);
        fgets(db[i][3], sizeof db[i][3], stdin);
        db[i][3][strcspn(db[i][3], "\n")] = 0;
        sfb;
    }
    printt(&db[0][0][0], i);
}

void printt(char *dbp, int slots){
    system("cls");
    printf("%-30s|%-20s|%-7s|%-10s\n", "Nombre", "Legajo", "Edad", "Antiguedad");
    for (int i = 0; i < slots * 80; i = i   80){
        printf("%-30c|%-20c|%-7c|%-10c\n", *(dbp i), *(dbp i 20), *(dbp i 40), *(dbp i 60));
    }
    printf("\n");
    system("pause");
}

Now, I have to clarify that I know that I'm using %c instead of %s in the printf()(Ln 48), but if I put %s, it won't compile, and it will give me the next error, 4 times:

format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Werror=format=]

I know that this happens because with the pointer, I'm only referencing to the first element of the array. My question is if there is anything that can tell the printf() to print where the pointer tells, and X (20) places more. In my mind I imagine something like this:

printf("%-30s|%-20s|%-7s|%-10s\n", *(dbp i)[20], *(dbp i 20)[20], *(dbp i 40)[20], *(dbp i 60)[20]);

I also know that I can use for() loops, to print each field, character by character, but I dont like how that looks :/

If there is no such thing as what I imagined, I'll be stuck to use the for() loop method.

CodePudding user response:

You don't need to dereference the pointer, and you can pass dpb[0][0] rather than the address of dbp[0][0][0].

You might want to look into using structs instead of multi-dimensional arrays, it'll make the logic a lot simpler.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define sfb if(db[i][0][0] == 17 || db[i][1][0] == 17 || db[i][2][0] == 17 || db[i][3][0] == 17){break;}

void read();
void printt(char *, int);

int main(){
    read();
    return 0;
}

void read(){
    char db[20][4][20];
    memset(db, 0, sizeof db);
    printf("Ingrese ^Q para salir\n");
    int i;
    for(i = 0; i < 20; i  ){
        printf("Ingrese nombre de persona: ");
        fgets(db[i][0], sizeof db[i][0], stdin);
        db[i][0][strcspn(db[i][0], "\n")] = 0;
        sfb;
        printf("Ingrese legajo de %s: ", db[i][0]);
        fgets(db[i][1], sizeof db[i][1], stdin);
        db[i][1][strcspn(db[i][1], "\n")] = 0;
        sfb;
        printf("Ingrese edad de %s: ", db[i][0]);
        fgets(db[i][2], sizeof db[i][2], stdin);
        db[i][2][strcspn(db[i][2], "\n")] = 0;
        sfb;
        printf("Ingrese antiguedad de %s: ", db[i][0]);
        fgets(db[i][3], sizeof db[i][3], stdin);
        db[i][3][strcspn(db[i][3], "\n")] = 0;
        sfb;
    }
    printt(db[0][0], i);
}

void printt(char *dbp, int slots){
    system("cls");
    printf("%-30s|%-20s|%-7s|%-10s\n", "Nombre", "Legajo", "Edad", "Antiguedad");
    for (int i = 0; i < slots * 80; i = i   80){
        printf("%-30s|%-20s|%-7s|%-10s\n", (dbp i), (dbp i 20), (dbp i 40), (dbp i 60));
    }
    printf("\n");
    system("pause");
}
  • Related