Solved

subscripted value is neither array nor pointer

Posted on 2009-04-02
8
2,403 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

776 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