954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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:

-bash-2.05b$ gcc project4a.c -lpthreads
project4a.c: In function `main':
project4a.c:32: subscripted value is neither array nor pointer
project4a.c:33: subscripted value is neither array nor pointer


in regards to these lines:

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


it is declared correctly, like this:

const int MAX = 1024;
int n[51];
int tot_n;

void *numsort (void *arg);


int main()
{
  char readnum[MAX];
.
.
.


what error is that???? anyone?

jaeSun
Light Poster
31 posts since Oct 2004
Reputation Points: 12
Solved Threads: 0
 

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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:

const int MAX = 1024;
int nums[50];
int tot_n;


my locals:

char readnum[MAX];
  int x = 0;
  int i, j, k, l, m, n;
  char *ifEOF;
  int numof_stgs;
  int numof_threads;
  pthread_t threadID;
  void *thread_result;


i changed it since, and it worked...

but then, now im getting the error again elsewhere:

-bash-2.05b$ gcc project4a.c -lpthread
project4a.c: In function `numsort':
project4a.c:102: warning: assignment makes integer from pointer without a cast
project4a.c:104: invalid operands to binary -
project4a.c:108: array subscript is not an integer
project4a.c:110: array subscript is not an integer
project4a.c:111: array subscript is not an integer
93:  void *numsort(void *arg)
94:  {
95:  
96:    //compare and switch if needed
97:  
98:    int *index = (int*)arg;
99:  
100:    int last, tmp, next;
101:  
102:    next = index + 1;
103:  
104:    last = tot_n - index;
105:  
106:    if (last == 2)
107:      {
108:        if (nums[index] > nums[next])
109:          {
110:            tmp = nums[index];
111:            nums[index] = nums[next];
112:            nums[next] = tmp;
113:          }
114:      }
115:    else
116:      {
117:        printf("checking last element\n");
118:      }
119:  
120:    printf("\n%i: Hello World!\n", arg);
121:  
122:    pthread_exit ((void*)NULL);
123:  }
jaeSun
Light Poster
31 posts since Oct 2004
Reputation Points: 12
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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!

jaeSun
Light Poster
31 posts since Oct 2004
Reputation Points: 12
Solved Threads: 0
 

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
#include

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

cmsc_illiterate
Newbie Poster
8 posts since May 2007
Reputation Points: 44
Solved Threads: 0
 

Don't go hijacking other people's threads. I already answered your question in your previous thread.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

Big deal. I'm new to this site, ok?

cmsc_illiterate
Newbie Poster
8 posts since May 2007
Reputation Points: 44
Solved Threads: 0
 

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

Thread closed.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You