Thread: pointers

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    17

    pointers

    hello guys,

    here is my problem


    if wer are adding a value to address like

    if the address location is 1002

    if we add 1002+1 what it will show

    if a[0]'s current address position is 1002

    main()
    {
    int a[2]={0,9};
    printf("%u",a[0]+1);

    }

    what is the o/p

    bye
    srinu

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >if we add 1002+1 what it will show
    It depends on the type of data this location is holding. Assuming the following:
    sizeof(char) = 1 (1002 + 1 = 1003)
    sizeof(int) = 4 (1002 + 1 = 1006)
    sizeof(long) = 8 (1002 + 1 = 1010)

    >main()
    >{
    >int a[2]={0,9};
    >printf("%u",a[0]+1);
    >}
    Output will be: 1
    That's because you are adding the contents at that location. Try (a + 1) instead.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Try it yourself....

    We arent here to do your homework

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Some tips:
    1. use int main() and return something
    2. we don't do homework
    3. use code tags:
    Code:
    &#91CODE&#93 /* your code */ &#91/CODE&#93

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Assuming you were going for something more like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int a[2] = {0,9};
      int *b = a;
      printf ( "%p\n%p\n", b, b + 1 );
      return EXIT_SUCCESS;
    }
    The output would be platform-dependent because the actual size of data types isn't specified by the C standard. On my system, the output of this program gave me

    0012FF78
    0012FF7C

    Which is consistent with an integer being 4 bytes on my computer.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM