Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

const qualifier in structure

Posted on 2009-04-09
2
Medium Priority
?
343 Views
Last Modified: 2012-05-06
I want to know why the const qualifier in front of the array at the start of this program, supposedly makes the program more reliable and readable. I think it's because the specifiers in the printf statements have an easier time accessing the contents of an array if the array itself is constant. Is that correct?
#include <stdio.h> #define LEN 20 
const char * msgs[5] =
{ 
     "    Thank you,", 
     "You prove that a ",
     "is a special guy. We must meet over a "
     "at"  
     "and laugh"
}
 
struct names {
char first[LEN];
char last[LEN];
};
 
struct guy {
      struct names handle
       char favfood[LEN];
      char job[LEN] 
      float income;
};
 
int main(void)
{
struct guy fellow = {
      {"Thomas", "Villard"},
     "grilled salmon",
      "life coach",
      58112.00
};
 
 
 
     printf("Dear %s, \n\n", fellow.handle.first); 
     printf("%s%s.\n", msgs[0], fellow.handle.first);
     printf("%s%s\n", msgs[1], fellow.job);
     printf("%s\n", msgs[2]);
     printf("%s%s%s", msgs[3], fellow.favfood, msgs[4]);
     if (fellow.income > 150000.0) 
         puts("!!"); 
      else if (fellow.income > 75,000)
         puts("!");
      else
               puts(".");
      printf("See you soon.\n");
 
return 0;

Open in new window

0
Comment
Question by:prebek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 2000 total points
ID: 24113030
Nothing to do with printf().

const specifies that the data shall not be changed. It encourages against bad coding practices, because if the compiler is strict, it won't allow passing a const as a non-const argument, without the programmer explicitly casting.

It simply tells that the array is of pointers to strings char are essentially readonly. A "const" string or array cannot be assigned to without casting to hide the true type of pointer, and even then, could result in a code crash even if, due to attempt to write to read-only memory.

The difference is illustrated below. It is not the array itself that is const, it is an array of pointers TO const strings.
const char * msgs[2] = { "test", "test" };
 
int main() {
  msgs[0] = "ABC";   // legal, can point to a NEW const string
  msgs[0][1] = 'A';  // illegal, what msgs[0] points to is not writeable
}

Open in new window

0
 

Author Closing Comment

by:prebek
ID: 31568804
thanks, i get it now
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question