Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C - passing parameter

Status
Not open for further replies.

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Hi,

How can we passing the parameter to the function?

.
.
array(3);
.
.
void array(const int size)
{
int Ary[size] = {0};
}

Is it not work on this code... How can i do on this code?? Anyone can help me...

Thank..
 

Array dimensions must be compile-time constants (unless you are using gcc, which provides an extended feature).
You'll have to use malloc or calloc, and remember to call free before the function returns.

What is array(3)?
 

    Help

    Points: 2
    Helpful Answer Positive Rating
echo47 said:
Array dimensions must be compile-time constants (unless you are using gcc, which provides an extended feature).
You'll have to use malloc or calloc, and remember to call free before the function returns.

What is array(3)?

Hi,

array(3); // is call function and need to pass the 3 to tht function...

I still not understand it. If i need to pass the array size from the function how can i do.... Please can you help me to modified the code?

Thanks Alot....
 

Code:
#include <stdlib.h>

void array(const int size)
{
  int *Ary;

  Ary = calloc(size, sizeof(*Ary));
  /* ... */
  free(Ary);
}
Don't forget to test for a possible error from calloc() because it could return NULL.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Hi,

How to test the error from calloc()???

The code i try like this but got error..... is it got missing something...on this code..

#include <stdlib.h>
void array(const int);

int main()
{
array(3);
return 0;
}//end main

void array(const int size)
{
int *Ary;

Ary = calloc(size, sizeof(*Ary));
/* ... */
free(Ary);
}
 

That compiles fine here. What error message do you get?

Here is an example with error checking. It also prints the array before and after writing some numbers into it:
Code:
#include <stdio.h>
#include <stdlib.h>

void array(const int size)
{
  int *Ary, n;

  Ary = calloc(size, sizeof(*Ary));
  if (!Ary)
  {
    printf("calloc failed\n");
    return;
  }

  for (n=0; n<size; n++)
    printf("%d ", Ary[n]);
  printf("\n");

  for (n=0; n<size; n++)
    Ary[n] = n + 100;

  for (n=0; n<size; n++)
    printf("%d ", Ary[n]);
  printf("\n");

  free(Ary);
}

int main(void)
{
  array(3);
  return 0;
}
Output:
0 0 0
100 101 102
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Hi,

I am using Microsoft Visual C++ compiler..


#include <stdio.h>
#include <stdlib.h>

void array(const int size)
{
int *Ary, n;

Ary = calloc(size, sizeof(*Ary)); // error C2440: '=': cannot convert from 'void *' to 'int *'
if (!Ary)
{
printf("calloc failed\n");
return;
}

for (n=0; n<size; n++)
printf("%d ", Ary[n]);
printf("\n");

for (n=0; n<size; n++)
Ary[n] = n + 100;

for (n=0; n<size; n++)
printf("%d ", Ary[n]);
printf("\n");

free(Ary);
}

int main(void)
{
array(3);
return 0;
}


the error show there.....
 

Your compiler thinks it's a C++ program, not a C program. The languages are different. Try to find a setting in VC++ that selects C mode.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Hi,

Nevermind, i will try later. What i can see is the 3 important line is there...

void array(const int size)
{
int *Ary;

Ary = calloc(size, sizeof(*Ary));
/* ... */
free(Ary);
}

Can you help me to explain this line what is it doing...
Ary = calloc(size, sizeof(*Ary));
and
free(Ary);

Thanks...
 

Ary = calloc(size, sizeof(*Ary));
The calloc function allocates space for an array of size objects, each of whose size is sizeof(*Ary). The space is initialized to all bits zero. The function returns either a null pointer (in case of error) or a pointer to the allocated space.

sizeof(*Ary)
That is the size of one element of Ary.

free(Ary);
The free function causes the space pointed to by Ary to be deallocated, that is, made available for further allocation.


You can probably eliminate the C++ error message like this:
Ary = (int*)calloc(size, sizeof(*Ary));

Be sure you've named your source file something.c and not something.cpp, because cpp may select the C++ compiler.
 

Q1)
What the different if we try to put
Ary = (int*)calloc(size, sizeof(int));
Why the sizeof(int) not int is *Ary??

Q2)
When this ....
if (!Ary)
{
printf("calloc failed\n");
return;
}
will occur?? Is it possible occur....

echo47 said:
Be sure you've named your source file something.c and not something.cpp.

Ya thanks alot... i change to .c file then it is work..

At this moment i stil 50% understand, nevermind let me play around the program that you give me and i try to understand from there... if got anything that i not understand i will try to futher discuss with you...

Thank you very much..
 

Q1: Your question is unclear, but I think you asking why I wrote sizeof(*Ary) instead of sizeof(int). Both are correct, but sizeof(*Ary) is better because if you someday change Ary from int* to a larger or smaller type such as double*, then sizeof(*Ary) will still be correct.

Q2: if calloc or malloc fails, it is usually because your computer has run out of memory. If your program fails to detect the error, and tries to use the array, the program will probably crash. That's a common bug in many programs.

You should search your compiler's library reference manual (or a good C book) for full descriptions of calloc, malloc, free, and other memory allocation functions.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Hi,

Now i understand the code already... Thank you very much....

Do you got any idea... if i apply this code at Keil Compiler for uC. I try already but got some worning there.... Bellow the code similar that you giving me (only the int *Ary; change it to int xdata *Ary; at Keil Compiler)

Warning Display:
Warning: Module Name Not Unique
Module: C:\Keil\C51\LIB\C51s.LIB (CALLOC)

Warning: Unresolved External Symbol
Symbol: _CALLOC
Module: calloc.obj (CALLOC)

Warning: Reference Made to Unresolved External
Symbol: _CALLOC
Module: calloc.obj (CALLOC)


Code:
#include <stdlib.h> 
#include <stdio.h>    
#include "reg52.h" 

void tst_calloc(const int size) 
{ 
   int xdata  *Ary; 
   int s=2, n=3; 

    Ary = calloc(size, sizeof(*Ary)); 
  
   if (!Ary) 
     { 
       return; 
     } 
  
   Ary[s]=n; 

//   free(Ary); 
} 

void main(void) 
{ 
   while(1) 
   { 
      tst_calloc(100); 
   } 

}


Thanks...
 

The answer is simple. You don't have calloc in the libraries which were searched when building your program. Check your manual and see which library you need to link with your program to include calloc. Also pay attention not to change your pointer when calling free or your program will crash.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top