• C

linker exercise - cannot get started

Hi,
This is my first posting.  This is  HW assignment and I am not asking for the outright answer. Yes, I know the experts could not do that anyway according to the rules.  I really want to understand this.  I'm at a loss at where to start.  The first hint is "what does p1.c need that is does not define?"  

I don't know.  My best guess is :

/*  PURPOSE:  To:
    (1) create an array of n integers with calloc(n,sizeof(int))
    (2) initialize this array with random integers with (rand() % 256)
    (3) call createAndCount() and store the returned sum
    (4) free() the array
    (5) return the sum
 */

I don't see anything in the existing code that would do those 5 items.  

Also, this is probably for later, but I'm not sure how this links with the other program (the whole purpose of the exercise).  But I will post a separate question later once I get a decent start going.  

Also, I have no idea how to award points - what is considered 500 points or 250 points.  
The program file p1.c below compiles under Linux to create an object file p1.o. It is to be linked with another file p2.o to create a running program, whole. It just adds two numbers.

/* p1.c
 */
#include <stdlib.h>
#include <stdio.h>


/**** Declarations go here: ****/

#define		STRING_LEN	256

/*  PURPOSE:  To:
    (1) create an array of n integers with calloc(n,sizeof(int))
    (2) initialize this array with random integers with (rand() % 256)
    (3) call createAndCount() and store the returned sum
    (4) free() the array
    (5) return the sum
 */
int		createAndCount	();



/**** Function and variables go here: ****/

/*  PURPOSE:  To keep track of the length of an array.
 */
int		n;



/*  PURPOSE:  To compute and return an arbitrary function from an array 'array'
 *      of 'n' integers.
 */
int		count	(int*	array)
{
  int	sum	= 0;
  int	i;

  for  (i = 0;  i < n-1;  i++)
    sum += array[i+1] - array[i];

  return(sum);
}



/*  PURPOSE:  To harass CSC 374 students.  Ignores parameters.  Returns
 *      'EXIT_SUCCESS' to OS.
 */
int	main	()
{
  char	text[STRING_LEN];

  do
  {
    printf("Please enter a positive integer: ");
    fgets(text,STRING_LEN,stdin);
    n = atoi(text);
  }
  while  (n <= 0);

  printf("The value is %d\n",createAndCount());
  return(EXIT_SUCCESS);
}

Open in new window

dazedandconfused69Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffric\Commented:
>> what does p1.c need that is does not define?

Easiest way to figure that out is to compile your post and look at the error message(s). I named your post add2numbers.c.

cc add2numbers.c
    undefined reference to `_createAndCount'

So I searched for createAndCount and saw your declaration:
>>    int  createAndCount  ();

I could have defined createAndCount() in the same file, but since you want another file, I created a file called createAndCount.c and put in a dummy stub:
int createAndCount() {
  return 1;
}

Open in new window


Then to link them together to produce an executable called add2num, as follows:
$ cc add2numbers.c createAndCount.c -o add2num

And then since createAndCount() always returns 1, don't expect the right answer; however, this is a reasonable start for you to get off the ground. The output is:
$ ./add2num
Please enter a positive integer: 4
The value is 1

Open in new window



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dazedandconfused69Author Commented:
Thanks!  It's a bit late where I am but I will try this solution tomorrow morning if I can't figure it out now and will respond as soon as I try it.  
0
dazedandconfused69Author Commented:
Thanks, this definitely got me starting.  This is my first class using C and I only have a limited java background so this is all very difficult for me.  I understand I'm supposed to define createandcount in a separate file and I get how to link those two at the end once I've written both of them.  Here are my prof's hints:

    (1) create an array of n integers with calloc(n,sizeof(int))
    (2) initialize this array with random integers with (rand() % 256)
    (3) call createAndCount() and store the returned sum
    (4) free() the array
    (5) return the sum

So starting with 1, I have questions:

(note: I am continuing your naming convention of add2numbers.c for the main program and createAndCount.c for the side program to be linked to the main program.

(a) is the array creation done inside the function createAndCount or in the add2numbers program?
(b) I researched calloc and if I understand correctly, the code I need should be something like:

      int* array;
      int NumElements = 1000;
      int SizeOfEachElement = sizeof(int);
      array = calloc(NumElements, SizeOfEachElement);


I have been reading up on calloc online and it is not clear to me.  This is also my first class with pointers and I won't claim I get them strongly either.  What kind of definition of createAndCount is expected in the add2numbers.c file?  I thought the point of linking was that I would define the function createAndCount in another file.  

Also, is there a FAQ on points?  I want to properly as possible award the correct amount of points.  
0
phoffric\Commented:
>> Thanks, this definitely got me starting.  This is my first class using C and I only have a limited java background so this is all very difficult for me.
   Glad to have helped you get started. No sense being hung up on how to build a project consisting of two or more .c files.
   Believe me that your limited java background will help you in C (although you will end up probably writing java code at times; but the compiler will guide you back to C - I've made that mistake a number of times myself).
   Learning C is difficult for everyone, so you are not alone. One language that is 10 times harder than C is C++ (although you can get by profesionally with only it being 4 times harder).

>> Also, is there a FAQ on points?  I want to properly as possible award the correct amount of points.
   A fair question. For limited EE members, points cost $$, so use them wisely. For premium members, there are unlimited points so less wisdom needs to be applied.

   http://www.experts-exchange.com/help.jsp?hi=23#hs=8&hi=102

 I've looked up some additional FAQs for you that I think will be useful:

   http://www.experts-exchange.com/help.jsp?hi=23#hs=8&hi=99

   http://www.experts-exchange.com/help.jsp?hi=23#hs=8&hi=101

   http://www.experts-exchange.com/help.jsp?hi=23#hs=8&hi=504

   http://www.experts-exchange.com/help.jsp?hi=23  

   http://www.experts-exchange.com/Community_Support/New_to_EE/Q_25157027.html

   http://www.experts-exchange.com/help.jsp#hs=29&hi=403
   
0
dazedandconfused69Author Commented:
Do I need an extern declaration for this createAndCount function in add2numbers.c?  Or when I do the gcc that combines them both, does that do the equivalent of an extern?  I have never written a C declaration before so am confused about how to link add2numbers.c to createAndCount.c.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.