Like Tree2Likes
  • 2 Post By AndrewHunter

Neither Array nor Pointer.

This is a discussion on Neither Array nor Pointer. within the C Programming forums, part of the General Programming Boards category; Hey, I've got a problem with some code I wrote. I keep getting the error: "subscripted value is neither array ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    2

    Question Neither Array nor Pointer.

    Hey, I've got a problem with some code I wrote.
    I keep getting the error: "subscripted value is neither array nor pointer", I wont post the original code, 'cause it's kinda long, but I replicated the error with this:

    Code:
    #include <stdio.h>
    
    int main()
    {	
    	printf("\n");
    	int i;
    	char Guess;
    	char Ans[1][4]={"A""B""C""D"};
    	
    	printf("Enter Guess: ");
    	scanf("%c",&Guess);
    	printf("\n");
    	
    	for (i=0;i<4;i++)
    	{	if(Guess[i]==Ans[i])
    		{	printf("Letter %d is correct!\n",(i+1));
    		}
    	}
    	printf("\n");
    
    
    return 0;
    }
    The error points to
    Code:
    	if(Guess[i]==Ans[i])
    I'm kinda new to coding, and i suspect it is rather obvious, but... help!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,282
    Guess is of type char, so using the array subscript on it (as in Guess[i]) is invalid if i is an integral type (which it is).

    An array of X is not an X, so the two cannot be directly compared. You can loop and compare each element of an array with a value, but that's different.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    I would like to begin by saying great job on isolating your down to the simplest example of what gave you the error. This is an important part of the troubleshooting process that many people find difficult to do.

    Some things to note:

    1. Read our FAQ-Define main so you understand what those empty parenthesis mean.

    2. Based on your posted example code, you may find it of some value to read cprogramming.com's quick tutorial on arrays.

    3. Also, you may also find some value in a more verbose treatment of the subject: Chapter 4.1 - Arrays
    laserlight and tabstop like this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    2

    Talking

    Hey, I fixed the problem.

    The links were also really useful Andrew, so thanks, that was great.

    - Vic

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-28-2010, 01:44 AM
  2. Casting from a 1D array pointer to a scalar pointer
    By komkamol in forum C Programming
    Replies: 8
    Last Post: 09-25-2009, 01:44 AM
  3. pointer to pointer that points to a char array
    By steve1_rm in forum C Programming
    Replies: 2
    Last Post: 01-13-2009, 11:03 PM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. A pointer to a character pointer array... can't pass
    By Lynux-Penguin in forum C Programming
    Replies: 9
    Last Post: 10-12-2003, 10:53 PM

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