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

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Oct 2004
Posts: 31
Reputation: jaeSun is an unknown quantity at this point 
Solved Threads: 0
jaeSun jaeSun is offline Offline
Light Poster

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

 
1
  #1
Oct 24th, 2004
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:

  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:

  1. 32: n[x] = atoi(readnum);
  2. 33: printf("%i ", n[x]);

it is declared correctly, like this:

  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?
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,643
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 721
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #2
Oct 24th, 2004
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.
I'm here to prove you wrong.
Quick reply to this message  
Join Date: Oct 2004
Posts: 31
Reputation: jaeSun is an unknown quantity at this point 
Solved Threads: 0
jaeSun jaeSun is offline Offline
Light Poster

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

 
0
  #3
Oct 24th, 2004
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:

  1. const int MAX = 1024;
  2. int nums[50];
  3. int tot_n;

my locals:

  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:

  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.  

  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: }
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,643
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 721
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #4
Oct 24th, 2004
>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.
I'm here to prove you wrong.
Quick reply to this message  
Join Date: Oct 2004
Posts: 31
Reputation: jaeSun is an unknown quantity at this point 
Solved Threads: 0
jaeSun jaeSun is offline Offline
Light Poster

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

 
0
  #5
Oct 24th, 2004
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!
Quick reply to this message  
Join Date: May 2007
Posts: 8
Reputation: cmsc_illiterate is an unknown quantity at this point 
Solved Threads: 0
cmsc_illiterate's Avatar
cmsc_illiterate cmsc_illiterate is offline Offline
Newbie Poster

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

 
0
  #6
May 6th, 2007
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.
Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
0
  #7
May 6th, 2007
Don't go hijacking other people's threads. I already answered your question in your previous thread.
"Technological progress is like an axe in the hands of a pathological criminal."
Quick reply to this message  
Join Date: May 2007
Posts: 8
Reputation: cmsc_illiterate is an unknown quantity at this point 
Solved Threads: 0
cmsc_illiterate's Avatar
cmsc_illiterate cmsc_illiterate is offline Offline
Newbie Poster

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

 
0
  #8
May 7th, 2007
Big deal. I'm new to this site, ok?
Quick reply to this message  
Join Date: Jun 2006
Posts: 7,616
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #9
May 7th, 2007
So kid, try to be modest instead acting smart. This kind of attitude won't fetch you any help.

Thread closed.
I don't accept change; I don't deserve to live.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC