Mixing Pointers and Integers in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Adrian20XX
    New Member
    • Feb 2008
    • 11

    #1

    Mixing Pointers and Integers in C

    Hi,

    I need to mix pointers and integers, but can't figure out how to do it, so if any caritative soul can help me in here I'll really really appreciate it.

    How should I cast things in here?

    At the end is what I'd like to do.

    TIA & Regards ...


    Code:
    #include <stdlib.h>
    
    typedef long myinteger;
    typedef myinteger *mypointer;
    
    main()
    {
    	mypointer p;
    	myinteger myiTest;
    
    	p=malloc(100);
    
    	// Now I want to put in place 3 of p, a pointer to place 7 of p
    	p[3]=(myinteger)p+7;
    
    	// Now I want to put in the place pointed by p[3] a pointer to place 11 of p
    	*(p[3])=p+11;
    
    	// Now I want to put in the place pointed by the place pointed by p3 a number: 2
    	*(*(p[3]))=2;
    
    	// Now I want to access the place pointed by the place pointed by p3
    	myiTest=*(*(p[3]));
    
    	free(p);
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Have you run this through a compiler and executed using your debugger?

    What do you see?

    Comment

    • Adrian20XX
      New Member
      • Feb 2008
      • 11

      #3
      Originally posted by weaknessforcats
      Have you run this through a compiler and executed using your debugger?

      What do you see?
      I see plenty of errors, that's why I asked how should I cast things to do this properly.

      Here is the output:

      >test.c
      test.c(17) : error C2100: illegal indirection
      test.c(17) : warning C4047: '=' : 'myinteger' differs in levels of indirection from 'mypointer'
      test.c(20) : error C2100: illegal indirection
      test.c(20) : error C2100: illegal indirection
      test.c(23) : error C2100: illegal indirection
      test.c(23) : error C2100: illegal indirection
      1>Build log was saved at "file://BuildLog.htm"
      1>TestPointers - 5 error(s), 1 warning(s)
      ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Well, take the first on on line 17:

        Originally posted by Adrian20xx
        *(p[3])=p+11;
        The error is:
        Originally posted by Adrian20xx
        test.c(17) : error C2100: illegal indirection
        p is an array of long integers. So, p is a long* but p[3] is a long. That makes *(p[3]) illegal indirection since you can't dereference a long.

        Instead of all this typecasting, you might consider using the fact that the array is in the eye of the beholder. That is, create an array of long and then create a pointer to that array that is a pointer to an array of long pointers:

        [code=c]
        long arr[12]; //big enough for 3 long*
        long** parr = (long**)arr;
        [/code]

        Now arr[2] is the 3rd long in arr and parr[2] is the 3rd long* in arr.

        Of course, now you can't tell the addresses in the array from the integer values inthe array, but that's your problem.

        This should remove all the typecasting.

        Comment

        • hdanw
          New Member
          • Feb 2008
          • 61

          #5
          Pointers and integers are different sizes, types.

          On some compilers you can do integer arithmetic on pointers to get some calculated offsets, but this is not a good practice to get into, as your code becomes less maintainable, and less portable.

          Try to write things so that the compiler can understand what you are doing. Then your code will work properly.

          For example :

          Using a pointer to index an array;

          Code:
          int id[12];
          int * pint = id[0];
          
          pint++;
          pint will point to the second int in the array after the increment, if and only if the compiler supports pointer arithmatic. Not all compilers do, and you may end up pointing to part of the first int, and part of the second.

          Whenever posible, write it like this:
          Code:
          int id[12];
          int index = 0;
          index ++;
          When you pass index in to the subscript operator like this:

          id[index]; the compiler can set up checks to see that the index does not go beyond the 12 item length of the array.

          using pint in the previous example does not, and sooner or later you will end up with programming bugs that are difficult to locate and resolve.

          Good luck,

          Happy Coding.

          Dan -

          Comment

          • Adrian20XX
            New Member
            • Feb 2008
            • 11

            #6
            Originally posted by weaknessforcats
            Well, take the first on on line 17:



            The error is:


            p is an array of long integers. So, p is a long* but p[3] is a long. That makes *(p[3]) illegal indirection since you can't dereference a long.

            Instead of all this typecasting, you might consider using the fact that the array is in the eye of the beholder. That is, create an array of long and then create a pointer to that array that is a pointer to an array of long pointers:

            [code=c]
            long arr[12]; //big enough for 3 long*
            long** parr = (long**)arr;
            [/code]

            Now arr[2] is the 3rd long in arr and parr[2] is the 3rd long* in arr.

            Of course, now you can't tell the addresses in the array from the integer values inthe array, but that's your problem.

            This should remove all the typecasting.
            Thank you sooooo much, you pointed me in the right direction and now my program is working perfectly.

            Your post made me re-think what I was doing, and I understood that the array was a space of memory (I like space of memory better than array because array makes you think about a type definition that might not be the best for the problem) of either pointers or integers, and that I had first to get a proper type definition for the way I will access the space of memory later.

            So now, my type definition for accessing it is:
            typedef long *******mytype;

            As I really want to access the long that is pointed by the seventh pointer, my program is working perfectly, and I'm a happy man, thank you sooooo much again.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Yes, indeed.

              I wrote an article titled Arrays Revealed inthe C/C++ HowTos forum because there were a lot of questions about array structures an a general non-awareness that an array is just a space of memory, as you put it.

              Comment

              Working...