NO

Author Topic: HELP PLEASE  (Read 6669 times)

er

  • Guest
HELP PLEASE
« on: June 06, 2012, 01:48:27 AM »
Hello All:
I need help with passing single dimensional arrays to functions. Any help will be GREATLY appreciated. I  am having difficulty with the code below:

#include<stdio.h>
#include<stdlib.h>
int *array, x, num, sm;
int sum( int);

int main()
{
   printf("Enter number of elements\n");
   scanf("%d", &num);
   array = (int*) malloc(num * sizeof(int));
   for(x=0; x < num; x++)
   {
   printf("Enter value %d\n", x +1);
   scanf("%d", array +x);
   }
   sm= sum(*(array+x));
   printf("Sum is %d", sm);
   
int sum(int array)
   {
      int i, total =0;
      for(i=0; i < num; i++)
         total += *(array+i);
         
   }
/*for(x=0; x < num; x++)
      printf("The value at (array+%d) is %d\n", x, *(array + x));
   int total = 0;
   for(x=0; x < num; x++)
      total += *(array + x);
      printf("The sum is %d", total);*/
}

CommonTater

  • Guest
Re: HELP PLEASE
« Reply #1 on: June 06, 2012, 02:35:08 AM »
Hello All:
I need help with passing single dimensional arrays to functions. Any help will be GREATLY appreciated. I  am having difficulty with the code below:

#include<stdio.h>
#include<stdlib.h>
int *array, x, num, sm;
int sum( int);

int main()
{
   printf("Enter number of elements\n");
   scanf("%d", &num);
   array = (int*) malloc(num * sizeof(int));
   for(x=0; x < num; x++)
   {
   printf("Enter value %d\n", x +1);
   scanf("%d", array +x);
   }
   sm= sum(*(array+x));
   printf("Sum is %d", sm);
   
int sum(int array)
   {
      int i, total =0;
      for(i=0; i < num; i++)
         total += *(array+i);
         
   }
/*for(x=0; x < num; x++)
      printf("The value at (array+%d) is %d\n", x, *(array + x));
   int total = 0;
   for(x=0; x < num; x++)
      total += *(array + x);
      printf("The sum is %d", total);*/
}


Actually you need help understanding functions... Your sum() function is inside your main function... C does not support nested functions. 
 
« Last Edit: June 06, 2012, 02:42:54 AM by CommonTater »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: HELP PLEASE
« Reply #2 on: June 06, 2012, 03:03:07 AM »
Well, beside the nesting of the function that CommonTater already mentioned, there are quite a few more errors in this code.

As it is a common rule in forums like this not to do the homework for you, please take it step by step and tell us which error/warning message you have problems with/don't understand. Then someone will surely help you and explain that particular one to you...

Ralf

er

  • Guest
Re: HELP PLEASE
« Reply #3 on: June 06, 2012, 12:27:21 PM »
Thank you Ralf & CommonTater. I fixed the problem with the nesting fxn. i.e. my sum() inside my main(). However I receive this error "error #2144: Type error: pointer expected." when I run the code. Help/input would be greatly appreciated.

migf1

  • Guest
Re: HELP PLEASE
« Reply #4 on: June 06, 2012, 12:34:40 PM »
Since it seems you just started learning the language, perhaps you find useful some general notes.

For one, it is always better to make your code more comprehensive for humans, as opposed to 'machine optimized'. I'm saying so due to your attempt to access array elements using pointer arithmetic notation, instead of indexing notation. I'm including the rest the of the notes in the form of comments, in the following piece of code (taken from the code you have posted)...

Code: [Select]
/*  try to minimize to the bare minimum the usage of global variables,
    try instead to define & use needed vars locally to functions, and
    pass them as arguments to other functions for further processing
 */
int main( void )
{
    int *array = NULL;                     // try to make it a habit to initialize to NULL defined pointers that are not used immediately
    int n=0, i=0, sum=0;                   // in general try to always initialize all locally defined variables (at the very least it won't hurt)

   array = malloc(n * sizeof(int));        // no need to cast malloc (unless your compiler is seriously outdated)
   for (i=0; i < n; i++)                   // i is the most common idiom for array indices (x,y,z are common idiom for float vars)
   {                                       // n is the most common idiom for upper bound
       printf("Enter value %d\n", i+1);   
       scanf("%d", &array[i] );             // using array indexing notation (instead of pointer arithmetic)
   }

   sum = sum_elements( array, n );          // when not common idioms, try to use descriptive naming for vars/functions
...

The above are not mandatory, of course, but it really help in making your code readable, even for you (when revisting it after a while). It also helps in spotting bugs.

You may also find it useful to google for "ansi c coding style" or for "c coding style" in general (there are a few of them, depending on the dev platform).

Now for the array passing as an argument to functions, since arrays are essentially pointers to their 1st element (with some restrictions), you just pass the array to the function and then handle its elements normally. Changes made to them are also reflected to the caller of the function.

EDIT:
Typos (again) :(
« Last Edit: June 06, 2012, 02:39:24 PM by migf1 »

CommonTater

  • Guest
Re: HELP PLEASE
« Reply #5 on: June 06, 2012, 04:19:43 PM »
Thank you Ralf & CommonTater. I fixed the problem with the nesting fxn. i.e. my sum() inside my main(). However I receive this error "error #2144: Type error: pointer expected." when I run the code. Help/input would be greatly appreciated.

Repost your code as it is now...  We're not mind readers.


Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: HELP PLEASE
« Reply #6 on: June 06, 2012, 05:20:05 PM »
Thank you Ralf & CommonTater. I fixed the problem with the nesting fxn. i.e. my sum() inside my main(). However I receive this error "error #2144: Type error: pointer expected." when I run the code. Help/input would be greatly appreciated.

Repost your code as it is now...  We're not mind readers.
+1

Even after un-nesting your sum() function, I think I had to fix 3 or 4 more errors to get the program to compile and work. Again, we do not do the whole homework for you. Post a specific problem and we will help you with that...

One thing you need to understand what a pointer is and what makes it different from a "normal" variable and the differences in using either one in C...

Ralf

er

  • Guest
Re: HELP PLEASE
« Reply #7 on: June 07, 2012, 11:37:06 AM »
Thank you Ralf,Tater and migf1*. I guess I will just have to brush up on pointers. As migf1 alluded to I just started learning C and for the record this is not a homework assignment. Can anyone recommend any good online sources on pointers. Thanks.

*Thank you for general notes.

migf1

  • Guest
Re: HELP PLEASE
« Reply #8 on: June 07, 2012, 12:16:45 PM »
Thank you Ralf,Tater and migf1*. I guess I will just have to brush up on pointers. As migf1 alluded to I just started learning C and for the record this is not a homework assignment. Can anyone recommend any good online sources on pointers. Thanks.

*Thank you for general notes.

You are very welcome :)

"C Programming, a Modern Approach - 2nd Edition" by King is an excellent introductory textbook for the C language. It covers both the C90 and the C99 standards. With a bit of effort you may even find it online somewhere ;)

I wish this book was out when I was taught C. Back then the only available source was K&R's "The C Programming language", which is an excellent reference but not that good for newcomers, imho. Now it is also outdated, since it only covers the ANSI standard: C89/90... but it does include very interesting exercises. They are also available online, along with their solutions (but you have to google for them, since I don't have them bookmarked).

For Win32 programming, have a look at "Forger's Win32 Api Tutorial". A rather popular and really good source for starting. However, it requires you already have a solid knowledge of the language (as all GUI frameworks do).
« Last Edit: June 07, 2012, 12:18:34 PM by migf1 »

migf1

  • Guest
Re: HELP PLEASE
« Reply #9 on: June 07, 2012, 12:27:25 PM »
Oh, I just realized you asked specifically for pointers!

Here is a couple of links to get you started (once more, try googling for "c pointers tutorial", there's a huge variety of tuts out there, most of them are good):

http://pw1.netcom.com/~tjensen/ptr/pointers.htm
http://www.gamedev.net/page/resources/_/technical/general-programming/a-tutorial-on-pointers-and-arrays-in-c-r1697
http://www.eskimo.com/~scs/cclass/int/sx7.html (do not miss this one)

and here is an excellent source on function pointers (at some point it becomes rather advanced though):
http://www.newty.de/fpt/index.html

I hope they will help.
« Last Edit: June 07, 2012, 12:37:37 PM by migf1 »

CommonTater

  • Guest
Re: HELP PLEASE
« Reply #10 on: June 07, 2012, 03:39:48 PM »
I guess I will just have to brush up on pointers. As migf1 alluded to I just started learning C and for the record this is not a homework assignment. Can anyone recommend any good online sources on pointers. Thanks.

No offense is intended but I think you need to work on more than just pointers...

You won't learn C by reading a book the way you would read a novel and you certainly won't learn it by watching a few videos on You Tube ...  it takes active and deliberate study.

If you are just starting out in C, you should be following a book or extended tutorial.  For the most part these are laid out progressively so that each page and section introduces new concepts that build upon what you've already done.  Study each page, try the examples, mess with the code and don't move on until you are sure you understand the current topic.  Do this page by page through the whole book and you should come out the other end with a good beginning knowledge of C, ready to start taking on a few simple projects.

Migf1 has recommended a couple of pretty good books for you... I'd like to ad "Teach Yourself C in 21 days" which you should be able to find online or you can order from Amazon.  (Note: C, not C++ they are two different languages).


er

  • Guest
Re: HELP PLEASE
« Reply #11 on: June 09, 2012, 12:33:38 AM »
Thank you very much tater and migf1...I will look up the resources you suggested. YES! I do have to work on more than pointers ....Thanks again.

CommonTater

  • Guest
Re: HELP PLEASE
« Reply #12 on: June 09, 2012, 02:40:08 AM »
Thank you very much tater and migf1...I will look up the resources you suggested. YES! I do have to work on more than pointers ....Thanks again.

If you need help along the way, don't be afraid to ask...
So long as you're not asking us to do your homework for you :D