Home > Blockchain >  Trying to input in last pointer of array index used
Trying to input in last pointer of array index used

Time:11-30

I am trying to input names and save them in a pointer. then to add more customers starting in the last index I left off in the input before. but I cannot get it right. any tips? sorry for putting all of the code it is just for understanding better.

#include<stdio.h>
    #define SIZE 50
    
    int main(){
    int x;
    char name[SIZE][SIZE];
    char *namePtr[SIZE][SIZE];
    
    printf("enter no of customers ");
    scanf("%d", &x);
    
    puts("enter customer names");
    
    for(size_t i = 0, j = 0; i < x;   i,   j){
        scanf("%s", &name[i][j]);
        namePtr[i][j] = &name[i][j];
        }
        
        puts("\nPrinting using pointer\n");
        
        for(size_t i = 0, j = 0; i < x;   i,   j){
            printf("%s ", &*namePtr[i][j]);
        }
        puts("\n");
        
        int c;
        printf("2nd number of customers to add ");
        scanf("%d", &c);
        
        puts("enter customer names");
        
        for(size_t i = x , j = 0; i < c x;   i,   j){
            scanf("%s", &*namePtr[i][j]);
        }
        puts("printing customers combined\n");
        
        for(size_t i = 0, j = 0; i < x c;   i,   j){
            printf("%s ", &*namePtr[i][j]);
        }
        return 0;
    }

The results are Ali Mike Will (NULL) (NULL)

keeping in mind the first amount of customers is 3 the second is 2

CodePudding user response:

This declaration

char *namePtr[SIZE][SIZE];

does not make a sense.

You need to declare SIZE pointers to strings like

char *namePtr[SIZE];

As a result of the invalid declaration the following for loops also do not make a sens.

Moreover this for loop

for(size_t i = x , j = 0; i < c x;   i,   j){
    scanf("%s", &*namePtr[i][j]);
}

invokes undefined behavior because pointers in the array namePtr with indices starting from x are not initialized.

It seems what you need is something like the following.

#include <stdio.h>

#define SIZE 50

int main( void ) 
{
    char name[SIZE][SIZE];
    char * namePtr[SIZE];
    
    printf("enter no of customers ");

    int n = 0;
    scanf( "%d", &n );
    
    printf( "enter %d customer names\n", n );
    
    for ( int i = 0; i < n; i   )
    {
        scanf( "Is", name[i] );
        namePtr[i] = name[i];
    }
    
    puts( "\nPrinting using pointer" );
    
    for ( int i = 0; i < n; i   )
    {
        printf( "%s ", namePtr[i] );
    }
    putchar( '\n' );
    
    printf( "\n2nd number of customers to add " );

    int m = 0;
    scanf( "%d", &m );
    
    printf( "enter %d customer names\n", m );

    for ( int i = n; i < m   n; i   )
    {
        namePtr[i] = name[i];
        scanf( "Is", namePtr[i] );
    }
    
    puts( "\nprinting customers combined" );
        
    for ( int i = 0; i < n   m;   i )
    {
        printf( "%s ", namePtr[i] );
    }
    putchar( '\n' );
    
    return 0;
}

Try to run the program.

  • Related