Exemplo n.º 1
0
int main()
{
	int card_val = assign_val();



	
	return 0;
}
Exemplo n.º 2
0
int main()
{
  int t=100;
  int *p;
  int *q=&t;
  p=&t;
  //*p say as goto address stored in p
  //*p is the value stored at the memory address stored in the block p
  //t   |10|   at address 12345
  //p   |12345| at address 12353
  //*p  dereferences the address stored at p and fetches the value at that address.
  //that is why while running this code, only difference in o/p is values of &p &q are different.
  // because they are different pointer vars at different memory locations
  printf("&t = %u\t, *p = %d\t, &p = %u\t, p = %u\t\n",&t, *p, &p, p);

  printf("&t = %u\t, *q = %d\t, &q = %u\t, q = %u\t\n",&t, *q, &q, q);

  //////////////////arithmatic////////////////////////////


  int n[3] = {12,22,43};
  int *int_p = n; // for array , u can use name of array as reference to the start of the block of memory, where, array resides
  printf("++*int_p=%u\n", ++*int_p);//prints 13, * has preced over +, so forst derefer value at int_p, ie 12, the ++12 = 13

  printf("*++int_p=%u\n", *++int_p);//prints 22, *(++int_p) = *(&(n+1)) = 22, now int_p points to int_p+1, ie 4 bytes agter int_p,
                                    //now, int_p = &n[1]

  printf("*(int_p+1)=%u\n", *(int_p+1));//prints 43,

  printf("*(int_p+1)=%u\n", *(int_p+2));//prints garbage, since out of array print.


  //////////////////////pass by reference////////////////////////////
  int a=1, b=10;
  printer(&a, &b);
  printf("a=%d,b=%d\n",a,b);



  /////////////////return struct//////////////////////////////////
  //check out the struct and the function assign_val() at the top.
  //
  COORD *point; // declares a pointer of type COORD
  point = assign_val(); // assign_val returns a 'pointer' to COORD XXX not a COORD object, so assigned to a pointer

  printf("x=%f,y=%f,z=%f\n", (point)->x, point->y, point->z); // xyz are accessed in this fashion

  ///////////////////////////////////////////////////////alternate techniue
  COORD pnt;
  pnt = *assign_val();
  printf("x=%f,y=%f,z=%f\n", pnt.x, pnt.y, pnt.z);
  // so use '->' for accesing struct vars with a object pointer,
  // so use '.' for accesing struct vars with a normal object,

  ///////////////////array and pointers////////////////////////////

  char achr[] = {'R', 'o', 'n', '\0'};
  char *c_p;
   c_p= achr; // points to the first elem of char array
  printf ("name  = %c\n", *c_p);

  printf("Elems: ");
  for (int i=0;i<strlen(achr);i++)
  {
    printf("%c", *c_p);
    c_p++;
  }

  printf("\n");
/*
pointers and arrays are different:

A pointer is a variable. We can do
pa = a and pa++.
An Array is not a variable. a = pa and a++ ARE ILLEGAL.
*/
////////////////////////////////////////////////Array of Pointers///////////////////////////////////////

//declare array with text seperated by \n


char s_b[] = {"pallavi\n"};
char s_c[] = {"priyal\n"};

char s_a[] = {"nilesh\n"};
char *s_d = {"ronak\n"};//this is a legal operation

printf("String %d =%s\n",1,s_d);//Thats how u print a string using pointers


char *ptr_arr[] = {s_a, s_b, s_c, s_d};
for (int x = 0; x< 4; x++){
printf("String %d =%s\n",x+1,ptr_arr[x]);
}

printf("\n comp val:%d",strcmp(s_b,s_c));









  printf("\n");



  //////////////////////////////////////////////////END/////////////////////////////////////////////////
  return 0;
} 
Exemplo n.º 3
0
/*
 * Failover the gtm ------------------------------------------------------
 */
int
failover_gtm(void)
{
	char date[MAXTOKEN+1];
	char *stdIn;
	int rc;
	FILE *f;
	
	elog(INFO, "Failover gtm\n");
	if (!isVarYes(VAR_gtmSlave) || (sval(VAR_gtmSlaveServer) == NULL) || is_none(sval(VAR_gtmSlaveServer)))
	{
		elog(ERROR, "ERROR: GTM slave is not configured. Cannot failover.\n");
		return(1);
	}
	
	if (do_gtm_ping(sval(VAR_gtmSlaveServer), atoi(sval(VAR_gtmSlavePort))) != 0)
	{
		elog(ERROR, "ERROR: GTM slave is not running\n");
		return(1);
	}

	/* Promote the slave */
	elog(NOTICE, "Running \"gtm_ctl promote -Z gtm -D %s\"\n", sval(VAR_gtmSlaveDir));
	rc = doImmediate(sval(VAR_gtmSlaveServer), NULL, 
					 "gtm_ctl promote -Z gtm -D %s", sval(VAR_gtmSlaveDir));
	if (WEXITSTATUS(rc) != 0)
	{
		elog(ERROR, "ERROR: could not promote gtm (host:%s, dir:%s)\n", sval(VAR_gtmSlaveServer), sval(VAR_gtmSlaveDir));
		return 1;
	}

	/* Configure promoted gtm */
	if ((f = prepareLocalStdin(newFilename(stdIn), MAXPATH, NULL)) == NULL)
		return(1);
	fprintf(f,
			"#===================================================\n"
			"# Updated due to GTM failover\n"
			"#        %s\n"
			"startup = ACT\n"
			"#----End of reconfiguration -------------------------\n",
			timeStampString(date, MAXTOKEN+1));
	fclose(f);
	elog(NOTICE, "Updating gtm.conf at %s:%s\n", sval(VAR_gtmSlaveServer), sval(VAR_gtmSlaveDir));
	rc = doImmediate(sval(VAR_gtmSlaveServer), stdIn, "cat >> %s/gtm.conf", sval(VAR_gtmSlaveDir));
	if (WEXITSTATUS(rc) != 0)
	{
		elog(ERROR, "ERROR: could not update gtm.conf (host: %s, dir:%s)\n", sval(VAR_gtmSlaveServer), sval(VAR_gtmSlaveDir));
		return 1;
	}

	/* Update and backup configuration file */
	if ((f = prepareLocalStdin(stdIn, MAXPATH, NULL)) == NULL)
		return(1);
	fprintf(f,
			"#===================================================\n"
			"# pgxc configuration file updated due to GTM failover\n"
			"#        %s\n"
			"gtmMasterServer=%s\n"
			"gtmMasterPort=%s\n"
			"gtmMasterDir=%s\n"
			"gtmSlave=n\n"
			"gtmSlaveServer=none\n"
			"gtmSlavePort=0\n"
			"gtmSlaveDir=none\n"
			"#----End of reconfiguration -------------------------\n",
			timeStampString(date, MAXTOKEN+1),
			sval(VAR_gtmSlaveServer),
			sval(VAR_gtmSlavePort),
			sval(VAR_gtmSlaveDir));
	fclose(f);
	rc = doImmediate(NULL, stdIn, "cat >> %s", pgxc_ctl_config_path);
	if (WEXITSTATUS(rc) != 0)
	{
		elog(ERROR, "ERROR: could not update gtm.conf (host: %s, dir:%s)\n", sval(VAR_gtmSlaveServer), sval(VAR_gtmSlaveDir));
		return 1;
	}
	freeAndReset(stdIn);
	backup_configuration();

	/* Reconfigure myself */
	assign_val(VAR_gtmMasterServer, VAR_gtmSlaveServer); reset_var(VAR_gtmSlaveServer);
	assign_val(VAR_gtmMasterPort, VAR_gtmSlavePort); reset_var(VAR_gtmSlavePort);
	assign_val(VAR_gtmMasterDir, VAR_gtmSlaveDir); reset_var(VAR_gtmSlaveDir);
	assign_sval(VAR_gtmSlaveServer, "none");
	assign_sval(VAR_gtmSlavePort, "0");
	assign_sval(VAR_gtmSlaveDir, "none");
	assign_sval(VAR_gtmSlave, "n");

	return 0;
}