Variable changing value when control goes to MAIN

Discussion in 'C' started by Sabur, Jan 22, 2009.

  1. Sabur

    Sabur New Member

    Dec 25, 2008
    Likes Received:
    Trophy Points:
    Okay, here's my first post. I am originally a Fortran programmer and have legacy code that I'm working with but it is necessary to use and modify a C code (which I did not originally write) to read data from a file in a proprietary format and then transfer that data to the Fortran code.

    I am a C/C++ Newbie and this question I'm posing should be totally on the C/C++ side of things.

    I have posted the relevant code below (in C, not C++) and have used Word to add line numbers to make it easier to refer to lines. I hope I'm not breaking any rules by doing this. If so let me know. I've also highlighted in red relevent lines and variable names.

    I am trying to acquire the value of "numChannels" in line 72 by "assigning" it to "numchn" in line 73. This is all done in the "Helper Functions", outside of MAIN. I have declared "numchn" in line 34 as "extern int numchn;". This is done ahead of MAIN and as I understand it, this should make it global.

    When I use the debugger (Visual Studio 2005) everything is fine; "numchn" shows up in the debugger as "325" which is what it should be. However, when control is returned to Main (lines 35 to 46), "numchn" becomes a 9 digit nonsense integer and this is what is passed to Fortran.

    This nonsense integer shows up on the Fortran side so I don't think I have a pointer/address/value problem (originally "brainwashed" on Fortran, this pointer stuff is hard to get used to; and I'm sure the reverse is true in some analogous way :) ).

    How do I get a variable to retain it's value when control is returned to MAIN?


    BTW: I have snipped a lot of the code out below the MAIN function as indicated. I have also snipped some of the code above MAIN which I have not indicated, but I believe I've left the relevent stuff in. FYI.

    1.	//-----------------------------------------------------------------------------
    2.	//
    3.	//	This sample program shows how to read a DIAdem file.
    4.	//
    5.	//-----------------------------------------------------------------------------
    6.	//-----------------------------------------------------------------------------
    7.	// Include files
    8.	//-----------------------------------------------------------------------------
    9.	#include "nilibddc.h"
    10.	#include <stdlib.h>
    11.	#include <stdio.h>
    12.	#include <string.h> 
    13.	//-----------------------------------------------------------------------------
    14.	// Macros
    15.	//-----------------------------------------------------------------------------
    16.	#define ddcChk(f) if (ddcError = (f), ddcError < 0) goto Error; else
    17.	#ifdef nullChk
    18.	#undef nullChk
    19.	#endif
    20.	#define nullChk(p) if (!(p)) { ddcError = DDC_OutOfMemory; goto Error; } else
    21.	//-----------------------------------------------------------------------------
    22.	// Constants
    23.	//-----------------------------------------------------------------------------
    24.	static const char * FILE_PATH		= "C:\\ACS\\11_3_2008_2_57.tdms";
    25.	//-----------------------------------------------------------------------------
    26.	// Forward declarations
    27.	//-----------------------------------------------------------------------------
    28.	static int	ReadFile		(void);
    29.	static int	ReadGroups		(DDCFileHandle file);
    30.	static int	ReadChannels 	(DDCChannelGroupHandle group);
    31.	double 		GetAvgDataValue (unsigned __int64 numDataValues, double *data);
    32.	#define cols 400       //MJB
    33.	#define rows 30000     //MJB
    34.	[COLOR="Red"]extern int numchn;[/COLOR] //MJB
    35.	[B]MAIN (double *datastuffraw, double *datastuffeun, int *argc, int *[COLOR="Red"]numchn[/COLOR], char* str1, char* grpnm, int ivf_str1_len, int ivf_grpnm_len)   //MJB
    36.	{
    37.	int	ddcError = 0;
    38.	*argc = 2;  //MJB Simply added in attempt to pass an integer. Not integral to original code
    39.	ddcChk (ReadFile());
    40.	Error:
    41.	if (ddcError < 0)
    i.	printf ("\nError: %s\n", DDC_GetLibraryErrorDescription(ddcError));
    42.	else
    i.	printf ("\nNo errors.\n");
    43.	printf("End of program, press Enter key to quit\n");
    44.	getchar();
    45.	return 0;
    46.	}[/B]
    47.	//-----------------------------------------------------------------------------
    48.	// Helper functions
    49.	//-----------------------------------------------------------------------------
    50.	// Reads the file
    51.	static int ReadFile (void)
    52.	{
    53.	<lots of irrevelant(?)code snipped>
    54.	}
    55.	// Reads all the channel groups in a file
    56.	static int ReadGroups (DDCFileHandle file)
    57.	{
    58.	<lost of irrevelant(?) code snipped>
    59.	}
    60.	// Reads all the channels in a channel group
    61.	static int ReadChannels (DDCChannelGroupHandle group)
    62.	{
    63.	int						ddcError = 0;
    64.	unsigned int			i, numChannels, length;
    65.	unsigned __int64		numDataValues;
    66.	DDCChannelHandle		*channels = 0;
    67.	char					*property = 0;
    68.	double					*data = 0; //avgDataValue;
    69.	// Read all the channels in this channel group
    70.	ddcChk (DDC_GetNumChannels (group, &numChannels));
    71.	nullChk (channels = (DDCChannelHandle *) calloc (numChannels, sizeof (DDCChannelHandle)));
    72.	ddcChk (DDC_GetChannels (group, channels, [COLOR="Red"]numChannels[/COLOR]));
    73.	[COLOR="Red"]numchn = numChannels;[/COLOR]
    74.	for (i = 0; i < numChannels; ++i)
    i.	{
    ii.	// Read the channel name
    iii.	ddcChk (DDC_GetChannelStringPropertyLength (channels[i], DDC_CHANNEL_NAME, &length));
    iv.	nullChk (property = (char *) malloc (length + 1));
    v.	ddcChk (DDC_GetChannelProperty (channels[i], DDC_CHANNEL_NAME, property, length + 1));
    vi.	printf ("\n");
    75.	//	printf ("Channel #%d name property: %s\n", i+1, property);
    i.	free (property); 
    ii.	property = 0;
    76.	<lots of irrevalent(?) code snipped>
    77.	//		avgDataValue = GetAvgDataValue (numDataValues, data);  //MJB Orig code, don't need
    78.	//		printf ("Channel #%d number of data values: %I64d\n", i+1, numDataValues); //MJB Orig code, don't need
    79.	//		printf ("Channel #%d average data value: %.2f\n", i+1, avgDataValue);      //MUB  "    "      "    "
    80.	}
    81.	Error:
    82.	// Cleanup
    83.	if (data)
    i.	free (data);
    84.	if (channels)
    i.	free (channels);
    85.	if (property)
    i.	free (property);
    86.	return ddcError;
    87.	}
    88.	double GetAvgDataValue (unsigned __int64 numDataValues, double *data)
    89.	{
    90.	int 	i;
    91.	double 	sum = 0.0;
    92.	for (i = 0; i < numDataValues; i++)
    i.	sum += data[i];
    93.	return sum / (unsigned int)numDataValues;
    94.	}
  2. NewsBot

    NewsBot New Member

    Dec 2, 2008
    Likes Received:
    Trophy Points:
    Now I would suggest first you get some simple programs running and you would see the concept of Variables in C.

    Variables are global when they are declared outside of any function and need not be before main always but what you have done is absolutely correct.

    Now about the Value being persistent there are many things which you should know and one of them is scope of variable.

    In C any Variable declared has a scope before the closing braces } and so if you declare a variable inside a function the moment function ends the variable is out of scope and so its value is not persist in the calling function and so to do that you have couple of options.

    1. Pass a variable pointer in the function to update the variable declared in calling function.
    2. Return the Variable and assign it to the variable.

    Now when passing a variable to function the passing is by value and so you copy the variable values but when you pass the variable as pointer the value will persist.

    I hope I have helped you and not confused you more and shoot if you don't get anything/
  3. Sabur

    Sabur New Member

    Dec 25, 2008
    Likes Received:
    Trophy Points:
    Thanks for the response Shabbir.

    An simple example or two would be helpful. I have been reading and trying different things, unfortunatley I need to balance the time needed to learn C with the time needed for all the rest of the things I need to do, this C coding being an extremely small part of the whole project and one (the C part) being sort of forced on me.

    I don't know what you mean by this? Setting the variable to itself?

  4. NewsBot

    NewsBot New Member

    Dec 2, 2008
    Likes Received:
    Trophy Points:
    I meant something like this

    int func(int a, int b)
      return a+b;
    int main()
      int x = func(2,3);
    // x would be assigned based on the returned value from func
      return 0;

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice