?
Solved

subscripted value is neither array nor pointer

Posted on 2009-04-02
8
Medium Priority
?
2,448 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

649 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