help me plz

This is a discussion on help me plz within the C Programming forums, part of the General Programming Boards category; #include<stdio.h> main() { int *p,*q,a; p=(int *)1000; q=(int *)2000; a=q-p; printf("a=%d",a); } Code: Code: #include<stdio.h>...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    1

    help me plz

    #include<stdio.h>
    main()
    {
    int *p,*q,a;

    p=(int *)1000;
    q=(int *)2000;
    a=q-p;
    printf("a=%d",a);
    }
    Code:
    Code:
    #include<stdio.h>

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    Re: help me plz

    what is

    =(int *)


    ?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    1. Ask a question
    2. put the code tags around ALL of your code next time.

    Assuming it ever compiles, I bet you're wondering why the value of a is either 250 or 500 right, and not the 1000 you expected.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    46
    I'm taking a guess at what you are trying to do. The following works.

    Code:
    #include<stdio.h>
    main()
    {
        int x,y;
        int *p,*q,a;
    
        x=1000;
        y=2000;
    
        p=&x; /*set pointer p to hold the address of x*/
        q=&y;/*set pointer q to hold the address of y*/
        a=*q-*p; /*what is pointed to by q minus what is pointed to by p*/ 
        printf("a=%d",a);
        
    }

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Hi ,
    You cannot just assign numbers into pointers.. the numbers need some memory and the pointers need to be pointed to this memory allocation.. this seems to be your first post.. make your problem clearer in the future...

    And you name seems Indian... are you from India...

    bye
    vasanth

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by vasanth
    Hi ,
    You cannot just assign numbers into pointers.. the numbers need some memory and the pointers need to be pointed to this memory allocation.
    Yes you can. The value pointers store is simply a number. This number represents some portion of memory. It is a perfectly valid assignment, assuming you actually are pointing to some memory space (number) which you have access to use.

    The only reason that is not a "working" assignment in the first portion of code, is because those are just some arbitrary numbers they threw in there, which they don't have rights to play around with.

    Thus, if they then tried to modify what they were pointing at, the program would likely crash.

    Video memory was commonly addressed this way. A fixed value was passed to a pointer, that value was where the video memory started at. It was then tweaked directly.

    So in short, yes, it is legal. No, it probably won't work the way you think it should unless you have some way to know for sure what you're now pointing at. Most new OSs don't like you to directly access hardware, so direct value assignment like that is usually a BadThing(TM).

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Student Forever! bookworm's Avatar
    Join Date
    Apr 2003
    Posts
    132
    Hey Sapna!
    U've declared the pointers right,but u've not assigned them(or whatever u've attempted to do with them) with the correct syntax.
    To learn in detail how to work with pointers,read the Schaum Series book on C.But if u are in a hurry,get E Balagurusamy's book from your library.Its an Indian publication,but from your name,I belive u r an Indian too.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    U've declared the pointers right,but u've not assigned them(or whatever u've attempted to do with them) with the correct syntax.
    To learn in detail how to work with pointers,read the Schaum Series book on C.But if u are in a hurry,get E Balagurusamy's book from your library.Its an Indian publication,but from your name,I belive u r an Indian too.
    If U avoid stupid abbreviations when U type then U will appear smarter to readers.

    >read the Schaum Series book on C
    For the price, that book is good, but we also have to take into account that the price is dirt cheap.
    My best code is written with the delete key.

  9. #9
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Originally posted by Prelude
    If U avoid stupid abbreviations when U type then U will appear smarter to readers.

    >read the Schaum Series book on C
    For the price, that book is good, but we also have to take into account that the price is dirt cheap.
    tHAT WA QUITE A SHARP UNWATED REMARK...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. plz help me...
    By sweetchakri in forum C Programming
    Replies: 1
    Last Post: 03-03-2009, 10:50 PM
  3. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 08:54 AM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM

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