About arrays

This is a discussion on About arrays within the C Programming forums, part of the General Programming Boards category; Hello! I'm currently doing work with arrays, and am having trouble visualizing what is happening with my code. From what ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    About arrays

    Hello! I'm currently doing work with arrays, and am having trouble visualizing what is happening with my code. From what I understand,

    Code:
     char names[100][20];
    
                 y=0;
                 printf("Enter inventory type:"); scanf("%s", &z);
                 names[y][y]=z;
                 printf("[%d][%d]=%s", y, y, names[y][y]);
    0
    when fruit is is scanned in, looks like this...0 Fruits

    But when I try to print out fruits again from the array, I encounter a problem and the program is forced to close. Can someone please explain to me where I am going wrong?

    thanks,
    Alex

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Code:
    scanf("%s", &z);
    z is already a pointer. you don't need to use & on it.
    Code:
    names[y][y]=z;
    strings can't be copied by assignment. you have to use a loop or strcpy.
    Code:
    strcpy(names[y], z);
    printf("[%d]=%s\n", y, names[y]);

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't know what z is, so who knows what's going on there.
    names[y][y] is a single character and cannot hold an entire string. names[y] is a string, so it could hold an entire string. But names[y] is not assignable with = -- you have to use strcpy, or scanf directly into it.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Ok, I tried what you guys said but to no avail, did I misinterpret?

    Code:
     int y=0;
     char names[100][20], *z;
    
    
                 printf("Enter inventory type:"); scanf("%s", z);
                 strcpy(names[y], z);
                 printf("[%d]=%s", y, names[y]);

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    z is just a pointer to some random place in memory. that's wrong because you can't write to memory that isn't yours and memory isn't yours unless you ask for it explicitly. you have to point that pointer somewhere or make it an array. an array is better because you don't have to manage memory.
    Code:
    char names[100][20], z[20];

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    char *z;
    z has no memory assigned to it (well, z itself has 4 bytes, but when you pass z as a parameter to scanf, it is the CONTENT of those 4 bytes which is passed, that that is not deifned)

    Use
    Code:
    char z[20];
    instead.

    Or just don't use z at all, but pass in names[y] to scanf.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Yay it ran!

    Thanks a bunch =P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

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