C Array passed to a function: how to absolutely prevent it from being written? -


i prevent array of integer passed function being altered : use of const

  • prevent being affected (= or ++ : compile error = ok)

  • do not prevent being scanned ! (scanf : warnings compiler...how compile error...

is there way ?

edit 1: may not clear enough...i demonstrate that, when adding const, impossible modify content of array...but seems not impossible...

edit 2: conlusion answers : c, impossible prevent array of integer passed function being modified inside function (-wall compiler option produce error instead of warning)

i have read things placement of const not me.

thanks help.

example of code :

#include <stdio.h> #define mysize 4  void testreadonly1(const int t[]) {     unsigned int = 0;     (i=0; i<mysize; i++)     {       /*t[i] = 0;*/ /* error : assignment read-only location */     } } void testreadonly2(const int t[]) {     unsigned int = 0;     (i=0; i<mysize; i++)     {         printf("%d ",i);         scanf("%d",&t[i]); /* warning : writing constant object */     } } void testreadonly3(const int const t[]) {     unsigned int = 0;     (i=0; i<mysize; i++)     {         printf("%d ",i);         scanf("%d",&t[i]); /* warning : writing constant object */     } }  void show(const int t[])  {     unsigned int = 0;     printf("\n");     (i=0; i<mysize; i++)     {         printf("%d ",t[i]);     }     printf("\n");  } int main ( void ) {     int t[mysize];    /*testreadonly1(t);         show(t);*/     testreadonly2(t);         show(t);     testreadonly3(t);         show(t);     return 0; } 

scanf variadic function, int scanf(const char *restrict format, ...);, , lacks type information additional parameters pass it. c compilers these days have knowledge of printf/scanf-like behavior, , can try perform type-checking, warning when passing const object scanf. don't see way of making specific warning : writing constant object gcc warning error without making warnings errors -werror, though modifying const object technically undefined behavior.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -