0

How can I print an array of strings that point to integers in C?

For example, I want to print the names of the following array.

const int names[] {
    John,
    Jane,
    Susan
};

Ive had no success with:

for(int i = 0; i < 3; i++){
   printf("The names in question is %d", names[i]);}

Update: The array of names must be type integers that point to an age elsewhere. The expected result is to print the names in the array not the age.

JJA
  • 73
  • 4
  • `const int names[]` is an array of integers. To print an array of strings, you have to declare an array of strings first. – MikeCAT Jul 21 '21 at 22:24
  • Seems like you have experience with another language and are now trying C? You need to declare variable types also (in the loop, for example, you use use int i = 0 rather than i = 0) – ᴓᴓᴓ Jul 21 '21 at 22:27
  • @talfreds A function definition like `int main(void){ ... }` is also required. The full code is not posted, so `i` may be declared elsewhere. – MikeCAT Jul 21 '21 at 22:32
  • 1
    More serious problem is the absense of `=` between `names[]` and `{`. – MikeCAT Jul 21 '21 at 22:34

3 Answers3

1

I think you're aiming for

const char *names[] = { // note type of names
    "John",             // strings must be in quotes
    "Jane",
    "Susan"
};

for ( size_t i = 0; i < 3; i++ )
  printf( "The name in question is %s\n", names[i] );  // %s instead of %d

An alternate method where you don't need to know the number of elements:

const char *names[] = {
  "John",
  "Jane", 
  "Susan", 
  NULL
};

for ( const char **p = names; *p != NULL; p++ )
  printf( "The name in question is %s\n", *p );
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • 1
    `for ( const char *p = names; p != NULL; p++ )` this won't work well. You should rethink the type of `p` and check whether what is pointed at by `p` is `NULL` instead of `p` itself. – MikeCAT Jul 21 '21 at 22:48
  • 1
    I love this solution. You need to change the code slightly, however: `for ( const char **p = names; *p != NULL; p++ )`, and `printf( "The name in question is %s\n", *p );`. ALSO: it's unclear what the OP is really looking for. ("print an array of strings that point to integers in C"???) – paulsm4 Jul 21 '21 at 23:08
  • Goddammit. Fixed. – John Bode Jul 22 '21 at 02:09
0
int main()
{
    char *names[] = {
      "John",
      "Jane",
      "Susan"
    };

    size_t namesLen = sizeof(names)/sizeof(typeof(*names)); // will return total elements in array
    for (int i = 0; i < namesLen; i++)
    {
      printf ("The names in question is %s\n", names[i]);
    }
    
    return 0;
}

You need to initialize your array of strings and then you can print.

*NOTE: you need to know the largest name you will store

  • 6 in this case (5 letters of the name and the '\0')

OUTPUT

The names in question is John
The names in question is Jane
The names in question is Susan
codeLvr
  • 3
  • 4
0

Printing names of identifiers in C is difficult because they are for compilers and they won't usually be in the executable binary except for as debugging information.

You should consider storing the names as strings aside with the integers.

#include <stdio.h>

struct question {
    const char* name;
    int age;
};

struct question questions[] = {
    {"John", 10},
    {"Jane", 20},
    {"Susan", 30}
};

int main(void) {
    for(int i = 0; i < 3; i++){
        printf("The names in question is %s\n", questions[i].name);
    }
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 1
    Well that's a wierd DV. As far as the conundrum that is the OP I think you're the closest to what can be considered a solution. – anastaciu Jul 21 '21 at 23:14