Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7053605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:31:32+00:00 2026-05-28T03:31:32+00:00

Playing with pointers in C is fun (not really). I have several arrays of

  • 0

Playing with pointers in C is fun (not really).

I have several arrays of strings I want to declare in an easy way, preferably something like:

arrayOfStrings1 = {"word1", "word2", etc. };
arrayOfStrings2 = {"anotherword1", "anotherword2", etc. };
arrayOfStrings3 = etc.
etc.

Something similar to a translation array (but not quite), so I want to be able to swap between these during runtime. For that I want a pointer pointerToArrayOfStrings that I can swap like:

pointerToArrayOfStrings = arrayOfStrings1;
doStuff();
pointerToArrayOfStrings = arrayOfStrings2;
doSomeOtherStuff();

In my naive understanding of arrays of strings and pointers to these, this is what I tried:

// Danish transforms
const unsigned char* da_DK[] = {"b","bb","c","c","cc","d","dd","e","f","ff","g","gg","h","hh","j","j","jj","k","k","kk","l","l","l","l","ll","m","mm","n","n","nn","p","pp","r","r","r","rr","s","s","s","ss","t","t","tt","v","v","vv","æ"};

// British english transforms
const unsigned char* en_GB[] = {"a","a","a","a","a","a","a","a","a","a","a","a","a","age","ai","aj","ay","b","cial","cian","cian","dj","dsj","ea","ee","ege","ei","ei","eigh","eigh","f","f","f","g","g","gs","i","i","i","j","j","k","ks","kw","l","m","n","n","o","r","s","s","sd","sdr","sion","sion","sj","sj","tial","tion","tion","tj","u","u","u","u","w","ye","ye","z"};

    // More languages....

const unsigned char** laguageStrings;

// Assign language
if (streq(language, "da-DK")){
    laguageStrings= da_DK;
}
else if (streq(language, "en-GB")){
    laguageStrings= en_GB;
}
else
         return 0;
}

Language is a char * containing the language “en-GB”, “da-DK” etc., streq() is just a home brewed (somewhat faster than strcmp()) string comparison function.

Long story short, depending on compiler this approach may work, report compiler warnings or compile, but give unexpected results.

What would be the correct way to solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:31:32+00:00Added an answer on May 28, 2026 at 3:31 am

    There are two way of working with array of characters (strings) in C. They are as follows:

    char a[ROW][COL];
    char *b[ROW];
    

    Pictorial representation is available as an inline comment in the code.

    Based on how you want to represent the array of characters (strings), you can define pointer to that as follows

        char (*ptr1)[COL] = a;
        char **ptr2 = b;
    

    They are fundamentally different types (in a subtle way) and so the pointers to them is also slightly different.

    The following example demonstrates the different ways of working with strings in C and I hope it helps you in better understanding of array of characters (strings) in C.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define ROW 5
    #define COL 10
    
    int main(void) 
    {
        int i, j;
        char a[ROW][COL] = {"string1", "string2", "string3", "string4", "string5"};
        char *b[ROW];
    
        /*
    
        a[][]
    
          0   1   2   3   4   5   6     7    8   9
        +---+---+---+---+---+---+---+------+---+---+
        | s | t | r | i | n | g | 1 | '\0' |   |   |
        +---+---+---+---+---+---+---+------+---+---+
        | s | t | r | i | n | g | 2 | '\0' |   |   |
        +---+---+---+---+---+---+---+------+---+---+
        | s | t | r | i | n | g | 3 | '\0' |   |   |
        +---+---+---+---+---+---+---+------+---+---+
        | s | t | r | i | n | g | 4 | '\0' |   |   |
        +---+---+---+---+---+---+---+------+---+---+
        | s | t | r | i | n | g | 5 | '\0' |   |   |
        +---+---+---+---+---+---+---+------+---+---+
    
        */  
    
        /* Now, lets work on b */    
        for (i=0 ; i<5; i++) {
            if ((b[i] = malloc(sizeof(char) * COL)) == NULL) {
                printf("unable to allocate memory \n");
                return -1;
            }
        }
    
        strcpy(b[0], "string1");
        strcpy(b[1], "string2");
        strcpy(b[2], "string3");
        strcpy(b[3], "string4");
        strcpy(b[4], "string5");
    
        /*
    
           b[]              0   1   2   3   4   5   6    7     8   9
        +--------+        +---+---+---+---+---+---+---+------+---+---+
        |      --|------->| s | t | r | i | n | g | 1 | '\0' |   |   |
        +--------+        +---+---+---+---+---+---+---+------+---+---+
        |      --|------->| s | t | r | i | n | g | 2 | '\0' |   |   |
        +--------+        +---+---+---+---+---+---+---+------+---+---+
        |      --|------->| s | t | r | i | n | g | 3 | '\0' |   |   |
        +--------+        +---+---+---+---+---+---+---+------+---+---+
        |      --|------->| s | t | r | i | n | g | 4 | '\0' |   |   |
        +--------+        +---+---+---+---+---+---+---+------+---+---+
        |      --|------->| s | t | r | i | n | g | 5 | '\0' |   |   |
        +--------+        +---+---+---+---+---+---+---+------+---+---+
    
        */
    
        char (*ptr1)[COL] = a;
        printf("Contents of first array \n");
        for (i=0; i<ROW; i++)
            printf("%s \n", *ptr1++);
    
    
        char **ptr2 = b;
        printf("Contents of second array \n");
        for (i=0; i<ROW; i++)
            printf("%s \n", ptr2[i]);
    
        /* b should be free'd */
        for (i=0 ; i<5; i++)
            free(b[i]);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been playing with pointers to better understand them and I came across something
I am learning C and I am playing around with pointers and arrays. I
I have never been good at playing with pointers in C. But this time
Playing with log4net, I have seen the possibility to use a per-thread stack of
While playing around with regexps in Scala I wrote something like this: scala> val
Anyone else playing with ironruby? I have successfully got the IronRuby.Rails.Example project running on
Quick C question here. We've been playing with double, triple, even quadruple pointers recently.
As a non-C/C++ expert I always considered square brackets and pointers arrays as equal.
I am just playing around to understand smart pointers and trying to make mine
I have been playing around with some debugging and wrote some C code that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.