Some help with point adress

This is a discussion on Some help with point adress within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { int i; int a[10]; for(i = 0; i < 10; i++) a[i] = i; ...

  1. #1
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178

    Some help with point adress

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i; int a[10];
    
    	for(i = 0; i < 10; i++)
            	a[i] = i;
    
    
    /* what I wanna to do the same thing like this */
    
    
    Code:
           
        for(i = 0; i < 10; i++)
            printf("%d = %d\n",a+i, *(a+i));
        printf("\n\n");
    /* But in little difernet style. That is that I do not declare i. something like */
    Code:
        for(; *a < 10; /* a += sizeof(int)   this is what I dont know how to increment adress */)
    	printf("%d = %d\n",a+i, *(a+i));
        printf("\n\n");
    return 0; }
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  2. #2
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    Code:
    int * b=a;
    for(; *b < 10; ++b )
    printf("%d = %d\n",b, *(b));
    printf("\n\n");
    The most important aspect of declaring the type of the pointer is when you want to do pointer arithmatic. So, if you delcare "a" as an int* , and you say a++ , the compiler knows how to increment a , i.e. by an amount equal to the size of int

    Hope this helps you.

  3. #3
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    RoshanX
    all I got is
    Code:
          wrong type argument to increment
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int a[] = {1,2,3,4,5,6,7,8,9,10};
       int *p;
       for ( p = a; p < &a[sizeof a / sizeof *a - 1]; ++p )
       {
          printf("%p:%d\n", (void*)p, *p);
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    In my previous reply, "a" should be substituted by "b".

    Anyway,
    Code:
    for (;*b <10 ;++b)
    is not a good way to impose the termination condition. memory beyond the 10th element will have values, and you are not guranteed an exit. Why you can't increment a is because a is an array which is different from just a pointer to an integer.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, you're guarinteed the ability to test for the location of one past the end of the array. You can't use it, but you can test for it.
    Code:
    char array[ BUFSIZ ];
    char *p = NULL;
    
    for( p = array; p !=  array + BUFSIZ; p++ )
        ...do something...
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a floating point exception
    By SnertyStan in forum C Programming
    Replies: 13
    Last Post: 03-25-2008, 11:00 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. instantiation point discussion
    By George2 in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2008, 02:48 AM
  4. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  5. floating point binary program, need help
    By ph34r me in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 06:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21