From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29861 invoked by alias); 8 Aug 2008 06:13:13 -0000 Received: (qmail 29851 invoked by uid 22791); 8 Aug 2008 06:13:12 -0000 X-Spam-Check-By: sourceware.org Received: from ti-out-0910.google.com (HELO ti-out-0910.google.com) (209.85.142.191) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 08 Aug 2008 06:12:38 +0000 Received: by ti-out-0910.google.com with SMTP id i7so474207tid.20 for ; Thu, 07 Aug 2008 23:12:35 -0700 (PDT) Received: by 10.110.14.12 with SMTP id 12mr5223654tin.31.1218175955232; Thu, 07 Aug 2008 23:12:35 -0700 (PDT) Received: by 10.110.109.10 with HTTP; Thu, 7 Aug 2008 23:12:35 -0700 (PDT) Message-ID: Date: Fri, 08 Aug 2008 06:55:00 -0000 From: "Rohit Arul Raj" To: gcc-help Subject: odd behavior with Character Arrays MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2008-08/txt/msg00067.txt.bz2 Hi All, Compiler Version: gcc 4.1.2 and gcc 3.4.6 Test Case: unsigned int g = 0; unsigned int slen(const char* c) { int l = 0; while(*c != '\0') { ++l; ++c; } return l; } int main() { unsigned int t; unsigned char n[] = {'a', 'b', 'c', 'd'}; t = slen(n); g = slen(n); printf("\n t = %d, g1 = %d\n", t, g1); return 0; } I have a test case given above which is used to compute the string length of the character array. In the test case, both 't' and 'g' call the same function to compute the string length. But both these values are different and they are wrong also. With GCC 4.1.2, t = 7, g = 5 With GCC 3.4.6, t = 15, g = 5 This happens only when i don't provide the size of the array 'n'. If size of the array is given "unsigned char n[15] = {'a', 'b', 'c', 'd'};" then the values are proper t = 4 and g = 4. 1. Is this the expected behavior with GCC? 2. Can i get more details as to why if the size of the array is not provided the compiler does not insert an string terminator at the end of the array. This happens with both character as well as integer arrays. Regards, Rohit