943,983 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 26592
  • C++ RSS
Oct 24th, 2004
1

C++: compile error "subscripted value is neither array nor pointer"

Expand Post »
im compiling a basic sorting algorithm that utilizies POSIX threads (pthreads) ....

everything was compiling fine, until i put the thread implementation in ....

all of a sudden, it gives me this:

C++ Syntax (Toggle Plain Text)
  1. -bash-2.05b$ gcc project4a.c -lpthreads
  2. project4a.c: In function `main':
  3. project4a.c:32: subscripted value is neither array nor pointer
  4. project4a.c:33: subscripted value is neither array nor pointer
  5.  

in regards to these lines:

C++ Syntax (Toggle Plain Text)
  1. 32: n[x] = atoi(readnum);
  2. 33: printf("%i ", n[x]);

it is declared correctly, like this:

C++ Syntax (Toggle Plain Text)
  1. const int MAX = 1024;
  2. int n[51];
  3. int tot_n;
  4.  
  5. void *numsort (void *arg);
  6.  
  7.  
  8. int main()
  9. {
  10. char readnum[MAX];
  11. .
  12. .
  13. .

what error is that???? anyone?
Similar Threads
Reputation Points: 12
Solved Threads: 0
Light Poster
jaeSun is offline Offline
31 posts
since Oct 2004
Oct 24th, 2004
0

Re: C++: compile error "subscripted value is neither array nor pointer"

n is a highly suspect name for a global variable. Did you accidentally declare a local variable n that was not an array? The global n would be hidden by the local n and you would get that error.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 24th, 2004
0

Re: C++: compile error "subscripted value is neither array nor pointer"

Quote originally posted by Narue ...
n is a highly suspect name for a global variable. Did you accidentally declare a local variable n that was not an array? The global n would be hidden by the local n and you would get that error.
no, the only global variables i have are:

C++ Syntax (Toggle Plain Text)
  1. const int MAX = 1024;
  2. int nums[50];
  3. int tot_n;

my locals:

C++ Syntax (Toggle Plain Text)
  1. char readnum[MAX];
  2. int x = 0;
  3. int i, j, k, l, m, n;
  4. char *ifEOF;
  5. int numof_stgs;
  6. int numof_threads;
  7. pthread_t threadID;
  8. void *thread_result;

i changed it since, and it worked...

but then, now im getting the error again elsewhere:

C++ Syntax (Toggle Plain Text)
  1. -bash-2.05b$ gcc project4a.c -lpthread
  2. project4a.c: In function `numsort':
  3. project4a.c:102: warning: assignment makes integer from pointer without a cast
  4. project4a.c:104: invalid operands to binary -
  5. project4a.c:108: array subscript is not an integer
  6. project4a.c:110: array subscript is not an integer
  7. project4a.c:111: array subscript is not an integer
  8.  

C++ Syntax (Toggle Plain Text)
  1. 93: void *numsort(void *arg)
  2. 94: {
  3. 95:
  4. 96: //compare and switch if needed
  5. 97:
  6. 98: int *index = (int*)arg;
  7. 99:
  8. 100: int last, tmp, next;
  9. 101:
  10. 102: next = index + 1;
  11. 103:
  12. 104: last = tot_n - index;
  13. 105:
  14. 106: if (last == 2)
  15. 107: {
  16. 108: if (nums[index] > nums[next])
  17. 109: {
  18. 110: tmp = nums[index];
  19. 111: nums[index] = nums[next];
  20. 112: nums[next] = tmp;
  21. 113: }
  22. 114: }
  23. 115: else
  24. 116: {
  25. 117: printf("checking last element\n");
  26. 118: }
  27. 119:
  28. 120: printf("\n%i: Hello World!\n", arg);
  29. 121:
  30. 122: pthread_exit ((void*)NULL);
  31. 123: }
Reputation Points: 12
Solved Threads: 0
Light Poster
jaeSun is offline Offline
31 posts
since Oct 2004
Oct 24th, 2004
0

Re: C++: compile error "subscripted value is neither array nor pointer"

>no, the only global variables i have are
Those are different from your first post.

>my locals:
I see an n that's declared as int.

>next = index + 1;
This line is trying to compute an offset from index and assign it to next, but next is not a pointer.

>last = tot_n - index;
Subtracting a pointer from an integer makes no sense.

>if (nums[index] > nums[next])
index is a pointer, not an integer. Array indices must be integral.

>tmp = nums[index];
This is the same problem.

>nums[index] = nums[next];
And here as well.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 24th, 2004
0

Re: C++: compile error "subscripted value is neither array nor pointer"

well, that solved it.... im new to the whole pthreads ... i coerced the passed value wrong... needed to coerce it to int, which is what i thought i was doing ....

but now it works ...... thanks for the help!
Reputation Points: 12
Solved Threads: 0
Light Poster
jaeSun is offline Offline
31 posts
since Oct 2004
May 6th, 2007
0

Re: C++: compile error "subscripted value is neither array nor pointer"

I am so lost. I don't know what I'm doing. Can somebody help me? What I'm trying to do right now is just store ID numbers in an array to use them elsewhere in my program. I can't even do that right though. This is my first time taking computer science and I just don't get it.

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

int GetID( int students, int StoreID[41], int i);

int main(void)
{
int students;
int StoreID[41];
int i;

printf("Enter the number of students in the class:");
scanf("%d", &students);

GetID(students,StoreID[],i);

return EXIT_SUCCESS;

}


int GetID( int students,int StoreID[41], int i)
{

printf("Student\n");

for( i=0; i<students; i++)
{
printf("Enter student%d ID:", i+1);
scanf("%d", &StoreID[i]);

}

printf("%d\n", StoreID[i]);

return StoreID[i];

}
In function 'main':
15: Warning: passing argument 2 of 'GetID' makes pointer from integer without a cast.
Reputation Points: 44
Solved Threads: 0
Newbie Poster
cmsc_illiterate is offline Offline
8 posts
since May 2007
May 6th, 2007
0

Re: C++: compile error "subscripted value is neither array nor pointer"

Don't go hijacking other people's threads. I already answered your question in your previous thread.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
May 7th, 2007
0

Re: C++: compile error "subscripted value is neither array nor pointer"

Big deal. I'm new to this site, ok?
Reputation Points: 44
Solved Threads: 0
Newbie Poster
cmsc_illiterate is offline Offline
8 posts
since May 2007
May 7th, 2007
0

Re: C++: compile error "subscripted value is neither array nor pointer"

So kid, try to be modest instead acting smart. This kind of attitude won't fetch you any help.

Thread closed.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: Little help with gcc & gdb
Next Thread in C++ Forum Timeline: Message Handler In C++ Builder ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC