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 6732689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:40:49+00:00 2026-05-26T10:40:49+00:00

Studying the K&R book in C I had a few questions regarding complicated pointer

  • 0

Studying the K&R book in C I had a few questions regarding complicated pointer declarations and pointer-array relationships.

1) What exactly is the difference between

char amessage[] = "this is a string";

and

char *pmessage
pmessage = "this is a string"

and when would you use one or the other?

From my understanding the first one allocates some amount of memory according to the size of the string, and then stores the chars in the memory. Then when you access amessage[] you just directly access whatever char you’re looking for. For the second one you also allocate memory except you just access the data through a pointer whenever you need it. Is this the correct way of looking at it?

2) The book says that arrays when passed into functions are treated as if you gave the pointer to the first index of the array and thus you manipulate the array through manipulating the pointer even though you can still do syntax like a[i]. Is this true if you just created an array somewhere and want to access it or is it only true if you pass in an array into a function? For example:

char amessage[]= "hi";
char x = *(amessage + 1); // can I do this?

3) The book says the use of static is great in this particular function:

/* month_name:  return name of n-th month */
char *month_name(int n)
{
    static char *name[] = {
       "Illegal month",
       "January", "February", "March",
       "April", "May", "June",
       "July", "August", "September",
       "October", "November", "December"
   };
   return (n < 1 || n > 12) ? name[0] : name[n];
}

I don’t understand why exactly this is a good use of static. Is it because the char *name[] would get deleted after function return if it is not static (because its a local variable)? Then does that mean in c you can’t do stuff like:

void testFunction(){
    int x = 1;
    return x; 
}

Without x being deleted before you use the return value? (Sorry I guess this might not be a pointer question but it was in the pointer chapter).

4) There are some complicated declaration like

char (*(*x())[])()

I’m really confused as to what is going on. So the x() part means a function x that returns a pointer? But what kind of pointer does it return its just a “” without like int or void or w/e. Or does that mean a pointer to a function (but I thought that would be like (*x)())? And then after you add brackets (because I assume brackets have the next precedence)…what is that? An array of functions?

This kind of ties to my confusion with function pointers. If you have something like

int (*func)() 

That means a pointer to a function that returns an int, and the name of that pointer is func, but what does it mean when its like int (*x[3])(). I don’t understand how you can replace the pointer name with an array.

Thanks for any help!

Kevin

  • 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-26T10:40:50+00:00Added an answer on May 26, 2026 at 10:40 am

    1) What exactly is the difference between

    char amessage[] = "this is a string";
    

    and

    char *pmessage
    pmessage = "this is a string"
    

    and when would you use one or the other?

    amessage will always refer to the memory holding this is a string\0. You cannot change the address it refers to. pmessage can be updated to point to any character in memory, whether or not it is part of a string. If you assign to pmessage, you might lose your only reference to this is a string\0. (It depends if you made references anywhere else.)

    I would use char amessage[] if I intended to modify the contents of amessage[] in place. You cannot modify the memory that pmessage points to. Try this little program; comment out amessage[0]='H' and pmessage[0]='H'; one at a time and see that pmessage[0]='H'; causes a segmentation violation:

    #include <stdio.h>
    
    int main(int argc, char* argv[]) {
        char amessage[]="howdy";
        char *pmessage="hello";
        amessage[0]='H';
        pmessage[0]='H';
        printf("amessage %s\n", amessage);
        printf("pmessage %s\n", pmessage);
        return 0;
    }
    

    Modifying a string that was hard-coded in the program is relatively rare; char *foo = "literal"; is probably more common, and the immutability of the string might be one reason why.

    2) The book says that arrays when passed into functions are treated as
    if you gave the pointer to the first index of the array and thus you
    manipulate the array through manipulating the pointer even though you
    can still do syntax like a[i]. Is this true if you just created an
    array somewhere and want to access it or is it only true if you pass
    in an array into a function? For example:

    char amessage[]= "hi";
    char x = *(amessage + 1); // can I do this?
    

    You can do that, however it is pretty unusual:

    $ cat refer.c
    #include <stdio.h>
    
    int main(int argc, char* argv[]) {
        char amessage[]="howdy";
        char x = *(amessage+1);
        printf("x: %c\n", x);
        return 0;
    }
    
    $ ./refer
    x: o
    $ 
    

    At least, I have never seen a “production” program that did this with character strings. (And I’m having trouble thinking of a program that used pointer arithmetic rather than array subscripting on arrays of other types.)

    3) The book says the use of static is great in this particular
    function:

    /* month_name:  return name of n-th month */
    char *month_name(int n)
    {
        static char *name[] = {
           "Illegal month",
           "January", "February", "March",
           "April", "May", "June",
           "July", "August", "September",
           "October", "November", "December"
       };
       return (n < 1 || n > 12) ? name[0] : name[n];
    }
    

    I don’t understand why exactly this is a good use of static. Is it
    because the char *name[] would get deleted after function return if
    it is not static (because its a local variable)? Then does that mean
    in c you can’t do stuff like:

    void testFunction(){
        int x = 1;
        return x; 
    }
    

    Without x being deleted before you use the return value? (Sorry I
    guess this might not be a pointer question but it was in the pointer
    chapter).

    In this specific case, I believe the static is needless; at least GCC is able to determine that the strings are not modified and stores them in the .rodata read-only data segment. However, that might be an optimization with string literals. Your example with another primitive data type (int) also works fine because C passes everything by value both on function calls and function returns. However, if you’re returning a pointer to an object allocated on the stack then the static is absolutely necessary, because it determines where in memory the object lives:

    $ cat stackarray.c ; make stackarray
    #include <stdio.h>
    
    struct foo { int x; };
    
    struct foo *bar() {
        struct foo array[2];
        array[0].x=1;
        array[1].x=2;
        return &array[1];
    }
    
    int main(int argc, char* argv[]) {
        struct foo* fp;
        fp = bar();
    
        printf("foo.x: %d\n", fp->x);
        return 0;
    }
    
    cc     stackarray.c   -o stackarray
    stackarray.c: In function ‘bar’:
    stackarray.c:9:2: warning: function returns address of local variable
    

    If you change the storage duration of array to static, then the address that is being returned is not automatically allocated, and will continue to work even after the function has returned:

    $ cat staticstackarray.c ; make staticstackarray ; ./staticstackarray
    #include <stdio.h>
    
    struct foo { int x; };
    
    struct foo *bar() {
        static struct foo array[2];
        array[0].x=1;
        array[1].x=2;
        return &array[1];
    }
    
    int main(int argc, char* argv[]) {
        struct foo* fp;
        fp = bar();
    
        printf("foo.x: %d\n", fp->x);
        return 0;
    }
    
    cc     staticstackarray.c   -o staticstackarray
    foo.x: 2
    

    You can see where the memory allocation changes between stackarray and staticstackarray:

    $ readelf -S stackarray | grep -A 3 '\.data'
      [24] .data             PROGBITS         0000000000601010  00001010
           0000000000000010  0000000000000000  WA       0     0     8
      [25] .bss              NOBITS           0000000000601020  00001020
           0000000000000010  0000000000000000  WA       0     0     8
    $ readelf -S staticstackarray | grep -A 3 '\.data'
      [24] .data             PROGBITS         0000000000601010  00001010
           0000000000000010  0000000000000000  WA       0     0     8
      [25] .bss              NOBITS           0000000000601020  00001020
           0000000000000018  0000000000000000  WA       0     0     8
    

    The .bss section in the version without static is 8 bytes smaller than the .bss section in the version with static. Those 8 bytes in the .bss section provide the persistent address that is returned.

    So you can see that the case with strings didn’t really make a difference — at least GCC doesn’t care — but pointers to other types of objects, the static makes all the difference in the world.

    However, most functions that return data in function-local-static storage have fallen out of favor. strtok(3), for example, extracts tokens from a string, and if subsequent calls to strtok(3) include NULL as the first argument to indicate that the function should re-use the string passed in the first call. This is neat, but means a program can never tokenize two separate strings simultaneously, and multiple-threaded programs cannot reliably use this routine. So a reentrant version is available, strtok_r(3), that takes an additional argument to store information between calls. man -k _r will show a surprising number of functions that have reentrant versions available, and the primary change is reducing static use in functions.

    4) There are some complicated declaration like

    char (*(*x())[])()
    

    I’m really confused as to what is going on. So the x() part means a
    function x that returns a pointer? But what kind of pointer does it
    return its just a “” without like int or void or w/e. Or does that
    mean a pointer to a function (but I thought that would be like
    (*x)())? And then after you add brackets (because I assume brackets
    have the next precedence)…what is that? An array of functions?

    This kind of ties to my confusion with function pointers. If you have
    something like

    int (*func)() 
    

    That means a pointer to a function that returns an int, and the name
    of that pointer is func, but what does it mean when its like int
    (*x[3])()
    . I don’t understand how you can replace the pointer name
    with an array.

    First, don’t panic. You’ll almost never need anything this complicated. Sometimes it is very handy to have a table of function pointers and call the next one based on a state transition diagram. Sometimes you’re installing signal handlers with sigaction(2). You’ll need slightly complicated function pointers then. However, if you use cdecl(1) to decipher what you need, it’ll make sense:

           struct sigaction {
               void     (*sa_handler)(int);
               void     (*sa_sigaction)(int, siginfo_t *, void *);
               sigset_t   sa_mask;
               int        sa_flags;
               void     (*sa_restorer)(void);
           };
    

    cdecl(1) only understands a subset of C native types, so replace siginfo_t with void and you can see roughly what is required:

    $ cdecl
    Type `help' or `?' for help
    cdecl> explain void     (*sa_sigaction)(int, void *, void *);
    declare sa_sigaction as pointer to function
        (int, pointer to void, pointer to void) returning void
    

    Expert C Programming: Deep C Secrets has an excellent chapter devoted to understanding more complicated declarations, and even includes a version of cdecl, in case you wish to extend it to include more types and typedef handling. It’s well worth reading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am studying the book Accelerated C++ from Koenig & Moo. Exercise 8-2 ask
While studying C++ (and C) I had some particular doubts regarding the working of
Background info I have a query regarding a questions from Sierra & Bates, SCJP
I'm studying for a finite automata & grammars test and I'm stuck with this
After studying TCP/UDP difference all week, I just can't decide which to use. I
I am studying natural deduction as a part of my Formal Specification & Verification
I am just studying a few classes given to me by my lecturer and
I have been studying the modalforms & inline formsets but am not able to
Probably another dumb question that results from my studying in bad C++ book (I
I am studying for my finals & I was reading context free grammars article

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.