Thread: Pointers giving different results in GCC in Ubuntu

  1. #1
    Registered User
    Join Date
    Apr 2023
    Posts
    4

    Pointers giving different results in GCC in Ubuntu

    Dear all,
    Today I am dealing with a code. Looks like similar method and the way but when I run the code output is:
    [abcde] [5] [6]
    [abcde] [5] [8]
    What is the rule for indexing and referencing in pointer arithmetics. ptr1 and ptr2 64 bit reference to a char array. I am a little confused. Are there any idea for making this issue clear. I would be much appreciated.
    Code:
    #include<studio.h>
    #include<stdlib.h>
    #include<string.h>
    int main(){
        char ptr1[]="abcde";
        char *ptr2=malloc(sizeof(char)*6);
        strcpy(ptr2,ptr1);
        printf("[%s] [%ld] [%ld]\n",ptr1,strlen(ptr1),sizeof(ptr1);
        printf("[%s] [%ld] [%ld]\n",ptr2,strlen(ptr2),sizeof(ptr2);
        getchar();
        return (0);
    }

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main( void )
    {
      char ptr1[] = "abcde";
      char *ptr2 = malloc ( sizeof ptr1 );
    
      strcpy ( ptr2, ptr1 );
    
      // Notice the %zu format...
    
      // This will give you 6 bytes (sizeof is the size of the array).
      printf ( "[%s] [%zu] [%zu]\n", ptr1, strlen ( ptr1 ), sizeof ptr1 );
    
      // This will give you 4 (32 bits) or 8 (64 bits) bytes (sizeof is the size of the pointer).
      printf ( "[%s] [%zu] [%zu]\n", ptr2, strlen ( ptr2 ), sizeof ptr2 );
    
      // strlen() using both ptr1 and ptr2 are the same (5 chars).
    
      // Don't forget to free the allocated buffer.
      free ( ptr2 );
    
      getchar();
    }

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    An array is not a pointer, although it can seem like one sometimes.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main() {
     
        // Empty brackets tell the array to calculate it's own size
        // based on the number of elements in the initializer.
        // A string literal includes a hidden '\0' at the end,
        // so 'a' is auto-dimensioned to 27.
        char a[] = "abcdefghijklmnopqrstuvwxyz";
     
        // sizeof gives the size of the object in characters (bytes)
        // which is just what we need for a malloc
        char *p = malloc(sizeof a);
     
        strcpy(p, a);
     
        // The array is 27 bytes.
        // strlen doesn't count the terminating '\0' char.
        printf("[%s] %zu %zu\n", a, strlen(a), sizeof a);
     
        // 64-bit pointers are 8-bytes.
        printf("[%s] %zu %zu\n", p, strlen(p), sizeof p);
     
        // In general, char arrays used as strings have a capacity
        // and a currently-in-use size. 
        char b[100] = "abcdefg";  // capacity 99+'\0', current size: 7+'\0'
        // The size of the string is determined by the current position of
        // the first '\0' character. It is an error for string data to not
        // have a '\0' character in the underlying char array.
     
        // sizeof gives the size of the underlying array
        printf("[%s] %zu %zu\n", b, strlen(b), sizeof b);
     
        // although this size information is lost with a function call
        // since the array variable b "decays" to a pointer when passed
        // to a function. Size information needs to be passed separately.
        void func(char*, size_t);
        func(b, sizeof b / sizeof b[0]);
     
        return 0;
    }
     
    void func(char *b, size_t size) {
        printf("[%s] %zu %zu passed size: %zu\n", b, strlen(b), sizeof b, size);
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Apr 2023
    Posts
    4
    Thanks sir for the assist. Now issue is definitely clear. My observation matches with other's memories. Respectfully.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-11-2015, 11:20 PM
  2. Simple Prefix Programming giving weird results
    By tmac619619 in forum C Programming
    Replies: 1
    Last Post: 03-13-2014, 08:18 PM
  3. Returning results from a function using pointers
    By Magi in forum C Programming
    Replies: 4
    Last Post: 11-19-2012, 05:43 PM
  4. Something other than Ubuntu
    By Annonymous in forum Tech Board
    Replies: 16
    Last Post: 10-24-2011, 06:07 PM
  5. output for array of pointers is not giving correct values
    By nkrao123@gmail. in forum C Programming
    Replies: 8
    Last Post: 09-03-2011, 07:40 AM

Tags for this Thread