Thread: Accessing values stored by a pointer into an array

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    5

    Accessing values stored by a pointer into an array

    I am writing a C program to access a string into an array from a pointer array in Visual Studio 2010.

    Program is given below:-

    Code:
    #include <stdio.h>
    #include <string.h>
    void main()
    {
    char data_a[6],data_b[6]="ABcde",*ptr;
    ptr=&data_b[0];
    data_a[0]=*ptr;
    printf("%s",a);
    }
    I am getting some junk character first and then my actual data on console window.
    I want to where I am getting wrong and how to resolve it.
    Last edited by Saad Rafey; 03-08-2013 at 01:04 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Please edit your post and put [code][/code] tags around your code.
    Copy again from your code editor so indentation is preserved.

    Edit:
    Now get rid of the bold, and indent the code.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Please guide me in this regard

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Saad Rafey View Post
    I am getting some junk character first and then my actual data on console window.
    I want to where I am getting wrong and how to resolve it.
    Since nobody here is proficient at mindreading over the internet, nobody can help you.

    Why do I say that? That code, as posted, would not produce the behaviour you describe, as it would not even compile (making using of an undeclared variable named a). If you can't ask a coherent question (i.e. one in which code you post actually exhibits the behaviour you describe) nobody can help you.

    The first step to getting useful advice is to post code that actually exhibits the symptoms you describe.

    The only other advice I can give is unrelated to your question: main() returns int, not void.
    Last edited by grumpy; 03-08-2013 at 06:40 AM. Reason: Fixed a couple of silly but trivial typos
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by Saad Rafey View Post
    Please guide me in this regard
    Indented and formatted code should look like this
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing an image stored in an array.
    By Mr.Sellars in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2008, 11:46 AM
  2. where are static values stored?
    By cs32 in forum C Programming
    Replies: 36
    Last Post: 03-04-2008, 11:08 PM
  3. stored values in elements of array, help
    By houler in forum C Programming
    Replies: 5
    Last Post: 12-09-2004, 04:19 PM
  4. accessing array of structs values
    By WaterNut in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2004, 08:47 PM
  5. Can yoou return a range of values stored into a integer?
    By correlcj in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 04:24 PM

Tags for this Thread