Solved

subscripted value is neither array nor pointer

Posted on 2009-04-02
8
2,415 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
Industry Leaders: 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 opening and reading files in the C programming language.

749 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