and now pointers are pointing in every direction
Register

We are the best invite forum on the internet! Here you will find free invites, free seedboxes, free bonuses, and much more. Our members know the true meaning of sharing and have created a truly global bittorent community! Our site has the most up to date information on all private trackers and our members will guide you and introduce you to this truly secretive and enlightened club. Ready to get started? Register now!


Results 1 to 7 of 7
  1. #1

    Join Date
    Jan 2011
    Posts
    43

    Default and now pointers are pointing in every direction

    OK so here it is:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    main()
    {
    char names[20] = "\0";
    char * pNames = names;
    char * pList[3] = {"\0", "\0", "\0"};
    int i = 0;

    for (i = 0; i < 3; i++)
    {printf("Enter a name: \n");
    scanf("%19s", pNames);
    pList[i] = pNames;}
    for (i = 0; i < 3; i++)
    {printf("%s", pList[i]);}
    return 0;
    }

    it outputs the last name entered 3 times. What am I doing wrong. the intention here is to input 3 names and then output them one after the other.

    Thanks in advance for putting up with my noobish nature.



  2. To remove ads become VIP. Inquire about advertising here.
  3. #2

    Join Date
    Sep 2009
    Location
    Germany
    Posts
    211

    Default

    I don't really understand what you are trying to do with pNames and pList, but this should do what you want:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int main()
    {
    char names[3][20];
    int i = 0;

    for (i = 0; i < 3; i++)
    {
    printf("Enter a name: \n");
    scanf("%19s", names[i]);
    }
    for (i = 0; i < 3; i++)
    {
    printf("%s\n", names[i]);
    }
    return 0;
    }
    Why would you want 2 pointers (names, pNames) pointing to the same memory to achieve this?

  4. #3

    Join Date
    Jan 2011
    Posts
    43

    Default

    char names[3][20] so this is the trick. the first notation is number of strings and secone is string length. the way my book describes it is waaaaay different lol.

    ------------------------Post auto merged------------------------

    I just realized that this does not use a pointer. I'm trying to get familiar with them as they are supposed to act more like strings. at least acording to my book they do.
    Last edited by Mattfodder; January 16th, 2011 at 03:48 PM.

  5. #4

    Join Date
    Sep 2009
    Location
    Germany
    Posts
    211

    Default

    Quote Originally Posted by Mattfodder View Post
    I just realized that this does not use a pointer. I'm trying to get familiar with them as they are supposed to act more like strings. at least acording to my book they do.
    If you write "char arr[30]" then arr is a pointer. You could also write "char *ptr", but then you would have to allocate and free the memory you need yourself. With the first method it is done for you.
    Arrays are Pointers to the memory address where the data is stored in C.

  6. #5

    Join Date
    Jan 2011
    Posts
    43

    Default

    so if you are going to use a pointer array you need to use malloc?

  7. #6

    Join Date
    Sep 2009
    Location
    Germany
    Posts
    211

    Default

    Quote Originally Posted by Mattfodder View Post
    so if you are going to use a pointer array you need to use malloc?
    Yeah the memory at a pointers address isn't allocated for you.
    The exception is of course, when you declare arrays like above, but then you only use pointers implicit.

  8. #7

    Join Date
    Jan 2011
    Posts
    43

    Default

    I think I understand now. I didn't know you could nest like that. Thank you.

Similar Threads

  1. ISOHunt loses in court and now will suck
    By unclesalty in forum BitTorrent Discussion
    Replies: 12
    Last Post: April 10th, 2010, 07:24 AM
  2. What sort of files are always in demand?
    By BernardSK in forum BitTorrent Discussion
    Replies: 16
    Last Post: July 20th, 2009, 06:15 PM
  3. Show Thanks points in user info
    By SunSpyda in forum Suggestions
    Replies: 4
    Last Post: June 26th, 2009, 06:27 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •