Results 1 to 6 of 6

Thread: Problem in assigning value to pointer in C-program

  1. #1
    Join Date
    Nov 2009
    Posts
    131

    Problem in assigning value to pointer in C-program

    Hello friends,
    I have written following code in C-language. In that code I have problem in assigning value to pointer in C-program. Can anyone tell what is the problem in my code. For your information I have written code below:
    Code:
    int q = 5; 
    int *ptrs; 
    *ptrs = &q;
    Last edited by Juany; 16-02-2010 at 03:51 PM.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Problem in assigning value to pointer in C-program

    Hey you have written wrong code and that's why are getting problem in your program. You have written *ptrs = &q; instead of ptrs = &q and that's why your program is not running.

    Just use following code in your program.
    Code:
    int q = 5; 
    int *ptrs;
    ptrs = &q;

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Problem in assigning value to pointer in C-program

    Your code is wrong and that's why you are getting such type of problem. In your code ptrs is not initialized properly. As per my information assigning to *ptrs creates a memory violation. You don't have permission to assign an int*q to an int. You have to use ptrs = &q to fix this problem. After this you can do *ptrs = 7;

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Problem in assigning value to pointer in C-program

    From your code it seems that ptrs is pointed to the memory address that has 5 stored in it. In this case you have to write following code to fix this problem.

    Code:
    int q = 5; // I have create an int which hold 5
    int *ptrs; //I have create a pointer that can point to an int
    ptrs = &q;

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Problem in assigning value to pointer in C-program

    You have written wrong code and that's why you are getting such type of problem. I will tell where you have written wrong code. Look at following code:

    Code:
    *ptrs = &q;
    In above code *ptrs dereferences the pointer it means that *ptrs assigns something into the memory slot to which ptrs is pointing. But ptrs has not been initialized and for this reason it points to some unknown location in memory.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Problem in assigning value to pointer in C-program

    A pointer in C-language is usually reffered as an address. Just think that pointer as an address label, and the value as the actual house. For example:
    Code:
    int *ptrs; // this declares a pointer to an int
    In above code ptrs is essentially a memory address.
    Code:
    int k = 9; // this create a local int.
    Above code Declares an integer on the stack and sets the value of it to 9.
    In above manner you have to write following code to fix this problem.
    Code:
    int q = 5; 
    int *ptrs; 
    ptrs = &q;

Similar Threads

  1. C Program - Percentage using pointer
    By syabab33 in forum Software Development
    Replies: 1
    Last Post: 08-05-2012, 11:42 PM
  2. Changing Stylus pointer to normal Pointer in Tablet PC
    By Kelley in forum Portable Devices
    Replies: 3
    Last Post: 21-06-2011, 07:22 PM
  3. Replies: 5
    Last Post: 29-10-2010, 02:45 PM
  4. Differentiation between void pointer and null pointer
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 12:11 PM
  5. Problem in a function pointer
    By Rilex in forum Software Development
    Replies: 3
    Last Post: 23-05-2009, 10:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,527,458.62223 seconds with 17 queries