Solved

subscripted value is neither array nor pointer

Posted on 2009-04-02
8
2,386 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
  • 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now