2

I'm quite new to C and I'm having some problems with the syntax and pointers.

I have an array

int ar[6] = {2, 3, 6, 7, 1, 9};

and I have a pointer

int* p = ar;

In the output instead of printing out to which number the pointer is pointing at, I want to have a ^ underneath of that number. And I want it to move as the pointer moves.

I want the output to the like this:

The array = {2 3 6 7 1 9}
             ^

but I don't know how to have it skip the "The array = {" part

I'm just printing the array like this

printf("The array = { ");

for(int i=0; i< 6;i++){
            printf("%d ", ar[i]);
    }

And I'm moving the pointer with getchar(), so the input from the user.

p = &a[0];

c = getchar();
if(c =='a'){
    if(p == &ar[0]){  
        p--;    
    }

if( c=='d'){
   p++;
}

I don't know if there's and easier way to do this or not.

Gabby_987
  • 191
  • 11
  • 5
    Print as many spaces as there are in `The array = {`, then print enough spaces to point to the array element. – Barmar Oct 23 '15 at 14:56
  • Suggest: since the width of a number can be anywhere from 1 char to ~13 characters, the `printf( "%d ", ar[i]);` will make aligning the `^` difficult. However, if the width modifier is added to the %d, as in "%5d " then the right edge of each number will be a easily calculated value, without having to calculate the number of characters in each item printed from the array. – user3629249 Oct 24 '15 at 21:14

3 Answers3

3

You can try this -

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

int main(){

    int ar[6] = {2, 3, 6, 7, 1, 9};
    int* p = ar+2;
    const char *s="int ar[6] = {";   // starting part of string 
    printf("%s",s);                  // print string
    for(int i=0; i< 6;i++){
       printf("%d ", ar[i]);         // print array elements
    }
    printf("}\n");                   // get to next line
    size_t n=strlen(s);              // calculate length of declaration part
    for(int i=0;i<n;i++)
         printf(" ");                // print number of spaces

    for(int i=0; i< 6;i++){ 
      if(p==ar+i){
         printf("^");               // if true print ^
         break;
      }
      else 
         printf("  ");              // if not then print 2 spaces 
    }
}

Output

龚成玥
  • 111
  • 10
ameyCU
  • 16,489
  • 2
  • 26
  • 41
2

Refine the part that prints the numbers a bit.

// Use variables to help match the output
char const* prefix1 = "The array = { ";
char const* prefix2 = "              ";

// Print the numbers first.
printf("%s", prefix1);
for(int i=0; i< 6;i++){
   printf("%d ", ar[i]);
}
printf("\n");

Here's the code to print the ^ symbol. You can test the pointer value against the address of the elements of a and print the ^ symbol. This will work when the numbers are not limited to just one digit.

// Print the the ^ symbol at the right place
printf("%s", prefix2);
for(int i=0; i< 6;i++) {

   if ( p == &ar[i] ) {
      printf("^");
      break;
   }

   // Print the number to the temporary buffer.
   // If the length of the buffer is 6, we need to print 6 spaces.
   char temp[20];
   sprintf(temp, "%d ", a[i]);

   int len = strlen(temp);
   for ( int j = 0; j < len; ++j )
   {
      temp[j] = ' ';
   }
   printf("%s", temp);
}
printf("\n");
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Building on what @Barmar mentioned in the comment, here is what I would do.

int main() {
    const char text[] = "The array = { ";
    int print_offset[6];
    int ar[6] = {2, 3, 6, 7, 1, 9};
    char c;
    int i;

    print_offset[0] = printf("%s", text) + 1;
    for(i=0; i<5;i++){
        print_offset[i+1] = print_offset[i] + printf("%d ", ar[i]);
    }
    printf("%d }\n", ar[i]);

    i = 0;
    while(1) {
        c = getchar();
        if(c =='a'){
          i++;
          i %= 6;
          printf("%*c\r", print_offset[i], '^');
        }
        else if(c=='d'){
          i--;
          if(i < 0)
            i = 5;
          printf("%*c\r", print_offset[i], '^');
        }
    }
}

In print_offset I store the offset I have to print the ^ at, thanks to the return value of printf. Then I use the * width specifier in my printf to print the '^' at the previously calculated offset.

The advantage here is that this code works even if you have ints that take 2 or more characters to print.

Community
  • 1
  • 1
jayant
  • 2,349
  • 1
  • 19
  • 27