?
Solved

subscripted value is neither array nor pointer

Posted on 2009-04-02
8
Medium Priority
?
2,441 Views
Last Modified: 2012-05-06
When I enter the code in below, I click to compile and I get two identical error messages saying "error: subscripted value is neither array nor pointer."

Why is this? Thanks.
double copy_arr(srce, trgt, n)
{
 
	int i;
	for(i = 0; i < n; i++)
	srce[i] = trgt[i];
	return trgt;
}

Open in new window

0
Comment
Question by:prebek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24055972
what are srce and trgt?
they need to be arrays or pointers in in order for srce[i] or  trgt[i] to make any sense
0
 

Author Comment

by:prebek
ID: 24056018
Srce and trgt are both arrays, they are dummy variables in the copy_arr() function. This is how I defined them in the main() portion of the program:

      double source[SIZE] = {1.5, 2.4, 3.3, 4.2, 5.1};
      double target1[SIZE];
      double target2[SIZE];

Should I define them within the copy_arr function as well? If so, how?
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 24056088
You need to declare the parameters as arrays, and the count (n) as an int, as follows:

double copy_arr(double[] srce, double[] trgt, int n)
{
 
      int i;
      for(i = 0; i < n; i++)
      srce[i] = trgt[i];
      return trgt;
}

Jim
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:prebek
ID: 24056155
When I do that it says: syntax error before 'srce.' Copy_arr is still giving me problems. Here's the whole program:

#include <stdio.h>
# define SIZE 5
double copy_arr(srce, trgt, n);
double copy_ptr(srce, trgt, n);
int main ()
{

      double source[SIZE] = {1.1, 2.2, 3.3, 4.4, 5.5};
      double target1[SIZE];
      double target2[SIZE];
      copy_arr(source, target1, 5);
      copy_ptr(source, target2, 5);
      
      printf("%s\n", target1);
      return 0;      
      printf("%s\n", target2);
      return 0;
      
}


double copy_arr(srce, trgt, n)
{

      for(int i = 0; i < n; i++)
      srce[i] = trgt[i];
      return trgt;
}

double copy_ptr(srce, trgt, n)
{
      int i;
      for(i = 0; i < n; i++)
            *(&srce + i) = *(&trgt+i);
      return trgt;
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 24056795
the copy of your source seems to show that you didn't do that
0
 
LVL 1

Accepted Solution

by:
amvasquez01 earned 500 total points
ID: 24061982
You didn't declare the type of the function parameters.  If you don't declare a type, the compiler will try to assume one.  Of course, it's probably not the type you want.  So when the compiler sees that you're trying to subscript the parameter, it throws and error.

You also had your source and targets reversed as well as an extra return statement in your main function.

You also can't use %s in printf to print out doubles.

I think this is what your code should look like:

#include <stdio.h>

# define SIZE 5

double * copy_arr(double * srce, double * trgt, int n);
double * copy_ptr(double * srce, double * trgt, int n);

int main ()
{
      int i;
      double source[SIZE] = {1.1, 2.2, 3.3, 4.4, 5.5};
      double target1[SIZE];
      double target2[SIZE];

      copy_arr(source, target1, SIZE);
      copy_ptr(source, target2, SIZE);

     for(i = 0; i < SIZE; i++)
        printf("%f ", target1[i]);
      printf("\n");

    for(i = 0; i < SIZE; i++)
        printf("%f ", target2[i]);
    printf("\n");

      return 0;
}

double * copy_arr(double * srce, double * trgt, int n)
{
      for(int i = 0; i < n; i++)
        trgt[i] = srce[i];

      return trgt;
}

double * copy_ptr(double * srce, double * trgt, int n)
{
      int i;
      for(i = 0; i < n; i++)
            *(trgt+i) = *(srce+i);

      return trgt;
}

Note how you need to declare the parameters both in your function declarations at the top as well as in the function implementations at the bottom.
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 24065396
Sorry - This was posted in the C# zone. I thought you were writing C#.

Jim
0
 

Author Closing Comment

by:prebek
ID: 31566089
Thanks so much. Your recommendation allowed me to compile, error-free. Have a good one.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question